Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <QCoreApplication>
- #include <QList>
- #include <iostream>
- class KPlayer;
- static QList<KPlayer *> *playerList = new QList<KPlayer *>();
- class KPlayer
- {
- public:
- KPlayer(const std::string& s1, const std::string& s2)
- : name1(s1), name2(s2)
- {}
- ~KPlayer()
- {
- // Just un-comment list->removeAll(this)
- // and you'll see the crash
- // playerList->removeAll(this);
- std::cout << "Removing " << name1 << std::endl;
- }
- private:
- std::string name1;
- std::string name2;
- };
- int main(int argc, char *argv[])
- {
- QCoreApplication a(argc, argv);
- playerList->append(new KPlayer("Twist", "Oliver"));
- playerList->append(new KPlayer("Twist2", "Oliver2"));
- playerList->append(new KPlayer("Twist3", "Oliver3"));
- playerList->append(new KPlayer("Twist4", "Oliver4"));
- QList<KPlayer *>::iterator it = playerList->begin();
- QList<KPlayer *>::iterator it_e = playerList->end();
- for (; it != it_e; it++) {
- std::cout << "Count = " << playerList->count() << std::endl;
- delete (*it);
- }
- // Instead loop above qDeleteAll can be used.
- // Loop is used to print list count.
- // qDeleteAll(playerList);
- playerList->clear();
- delete (playerList);
- return a.exec();
- }
Advertisement
Add Comment
Please, Sign In to add comment