qt - What is the scope of a C++ class added to a QList -


i wrote small piece of code prove myself bad practice indeed bad practice. i'm not seeing result expected. have qlist storing objects rather pointers objects. here class:

class test{   public:   test(int value){val=value; printf("created test %d\n",val);}   ~test(){ printf("deleting test %d\n", val); val=0;}   void print(){printf("test %d!\n", val);}   int val; }; 

and have list of these test functions follows:

class myclass { ... qlist<test> _test_list; void do_test_init(); void do_test(); ... };  void myclass::do_test_init() {   for(int i=0; < 10; i++)     _test_list.append(test(i+100)); }  void myclass::do_test() {   for(int i=0; < 10; i++)     _test_list[i].print();     } 

the output see is:

created test 100 deleting test 100 created test 101 deleting test 101 created test 102 deleting test 102 created test 103 deleting test 103 created test 104 deleting test 104 created test 105 deleting test 105 created test 106 deleting test 106 created test 107 deleting test 107 created test 108 deleting test 108 created test 109 deleting test 109 test 100! test 101! test 102! test 103! test 104! test 105! test 106! test 107! test 108! test 109! 

i'm assuming because append function taking copy of object passed in. mean it's safe use qlist way? preference store pointers rather objects, misguided?

the answer question it's "safe" in sense object copied when appended list. providing copy constructor shows clearly. copied objects deleted when list cleared.


Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -