0

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.

Community
  • 1
  • 1
chikuba
  • 4,229
  • 6
  • 43
  • 75

1 Answers1

2

If I understand what you're asking, you're concerned because the QStringList &strings is passed by reference?

The initialization list calls the stringList(const &QStringList) copy-constructor. This copy constructor copies the state of the passed &QStringList object to a new object and assigns that object to stringList This way even if the original &strings goes out of scope or is otherwise destroyed it won't matter since stringList points to a different object.

In your second link there is no copy-constructor invoked... the member object is assigned to point at the same object that was passed to it. If the passed object is destroyed later in the program the member object would still be pointing to that destroyed object, making it invalid.

  • wait what? I can see no difference between: myclass(const std::string& _name) : name(_name) {} and StringListModel(const QStringList &strings, QObject *parent = 0) : QAbstractListModel(parent), stringList(strings) {} Can you explain that? – chikuba Feb 16 '12 at 01:44
  • 1
    I think he meant to say "your second link", not first. There would only be a problem with your example if you were **storing** a reference to the given QStringList, not just using the reference which is then *copied* into your member variable. – Chris Feb 16 '12 at 03:28
  • @Chris Good catch. I meant second link. –  Feb 16 '12 at 05:39
  • but what would happen if I, in the constructor did "stringlist = strings". This is something I see a lot in different examples when dealing with QWidgets, Qlist etc.. I read somewhere that the =operator for thoses objects pretty much assigns the objects different values to those in the object on the right-hand-side of the =. Is this true? I'm not getting this at all which is really annoying. & will always be a memoryadress in my mind... – chikuba Feb 16 '12 at 07:45
  • Short answer is that the compiler should throw an error. `stringList = strings` is attempting to assign a reference `const QStringList& strings` to a non-pointer type `QStringList stringList`. –  Feb 16 '12 at 07:57
  • Michael: ?? stringList = strings calls the assignment operator (operator=) of of stringList, assigning strings. That's completely valid of course. Both operator= and copy actor create a copy of the passed object (or rather, in this case they don't, as QStringList is implicitly shared, but that's opaque to the using code) and don't store a reference or pointer, so it doesn't matter if you have a const ref or a value. – Frank Osterfeld Feb 16 '12 at 22:00