Pregunta

Tener una clase que tenga las siguientes funciones:

FileInfoWrapper(const QFileInfo &_fileInfo) : fileInfo(_fileInfo) {}

const QString& FileName() const { return fileInfo.fileName(); }

Pero cuando hago esto:

QFileInfo info(somePath);

qDebug() << info.absoluteDir(); // works

FileInfoWrapper test(info);

qDebug() << test.FileName(); // this crashes the entire application

Cuando elimino el const y de la devolución de la cadena, funciona. Es como << no funciona con referencias. ¿Qué pasa y por qué se bloquea?

¿Fue útil?

Solución

You return reference to the QString which is destroyed when you leave FileName() function.

Otros consejos

std::cout doesn't know QString, you need to convert it to std::string or const char*

Use QString::toStdString to convert to std::string, e.g.:

std::cout << test.FileName().toStdString();
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top