Im creating a model in qt and came across this code:
class StringListModel : public QAbstractListModel
{
Q_OBJECT
public:
StringListModel(const QStringList &strings, QObject *parent = 0)
: QAbstractListModel(parent), stringList(strings) {}
int rowCount(const QModelIndex &parent = QModelIndex()) const;
QVariant data(const QModelIndex &index, int role) const;
QVariant headerData(int section, Qt::Orientation orientation,
int role = Qt::DisplayRole) const;
private:
QStringList stringList;
};
Now I wonder, does this work? If a pass a qstringlist into this function thats localeted of the stack, and it runs out of scope, wont this object loose its stringlist?
Read both: C++ reference in constructor and: Constructors accepting string reference. Bad idea?
.. where some people say that it will be invalid, but some say that the string (in their examples) will be copied to the local variable. This is really confusing.