I have a class like this:
class SomeClass {
public:
QString data;
SomeClass(const QString &);
};
and in the .cpp file:
SomeClass::SomeClass(const QString &_data) {
data = _data;
}
then I use it like this:
SomeClass c("foobar");
This will call QString(“foobar”) and will pass it to the constructor of the SomeClass. But this QString is an automatic object so its lifetime will be gone after the constructor. On the other hand data won’t be copied(as far as I understand Qt docs) unless modified. Is it possible to have lifetime issues with this code or am I wrong? How to design it better?
All is OK. When you destroy your temporary QString data is modified, so copy would be performed. Data modification means that it will be modified at any side, in string that references data or in string that contains data.