Guest User

Untitled

a guest
Sep 22nd, 2012
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.30 KB | None | 0 0
  1. #include <QCoreApplication>
  2. #include <QList>
  3. #include <iostream>
  4.  
  5. class KPlayer;
  6.  
  7. static QList<KPlayer *> *playerList = new QList<KPlayer *>();
  8.  
  9. class KPlayer
  10. {
  11. public:
  12.     KPlayer(const std::string& s1, const std::string& s2)
  13.         : name1(s1), name2(s2)
  14.     {}
  15.     ~KPlayer()
  16.     {
  17.         // Just un-comment list->removeAll(this)
  18.         // and you'll see the crash
  19.         // playerList->removeAll(this);
  20.         std::cout << "Removing " << name1 << std::endl;
  21.     }
  22.  
  23. private:
  24.     std::string name1;
  25.     std::string name2;
  26.  
  27. };
  28.  
  29. int main(int argc, char *argv[])
  30. {
  31.     QCoreApplication a(argc, argv);
  32.  
  33.     playerList->append(new KPlayer("Twist", "Oliver"));
  34.     playerList->append(new KPlayer("Twist2", "Oliver2"));
  35.     playerList->append(new KPlayer("Twist3", "Oliver3"));
  36.     playerList->append(new KPlayer("Twist4", "Oliver4"));
  37.  
  38.     QList<KPlayer *>::iterator it = playerList->begin();
  39.     QList<KPlayer *>::iterator it_e = playerList->end();
  40.  
  41.     for (; it != it_e; it++) {
  42.         std::cout << "Count = " << playerList->count() << std::endl;
  43.         delete (*it);
  44.       }
  45.  
  46.     // Instead loop above qDeleteAll can be used.
  47.     // Loop is used to print list count.
  48.     // qDeleteAll(playerList);
  49.     playerList->clear();
  50.     delete (playerList);
  51.    
  52.     return a.exec();
  53. }
Advertisement
Add Comment
Please, Sign In to add comment