My QList contains pointers to a class. I am trying to iterate through a QList, but all of its elements seem to contain only the last pointer assigned by QList.
the code:
QList<fooClass*> myList; fooClass *pNewFooA = new fooClass("Bob"); myList.append(pNewFooA); fooClass *pNewFooB = new fooClass("Jen"); myList.append(pNewFooB); fooClass *pNewFooC = new fooClass("Mel"); myList.append(pNewFooC); QList<fooClass*>::iterator i; for (i = myList.begin(); i != myList.end(); i++) { cout << (*i)->getName() << "\n"; }
exit:
Mel Mel Mel
Instead of using .append (), I also tried the following, but this did not work:
- myList.push_back ("Zoe");
- myList <"Zoe";
- myList + = "zoe";
fooClass.cpp
QString name = ""; QString fooClass::getName() { return name; } fooClass::fooClass(QString newName) { name = newName; }
source share