Results 1 to 9 of 9

Thread: Objects and members

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Objects and members

    Maybe something like this will do:

    Qt Code:
    1. class xyz {
    2. public:
    3. static xyz *create(){ return new xyz(); };
    4. ~xyz(){ int ind = xyz::_instances.indexOf(this); xyz::_instances[ind]=0; }
    5. void doSomething(uint ind){
    6. if(xyz::_instances.size()<=ind || xyz::_instances.value(ind)==0)
    7. return;
    8. xyz::_instances[ind]->inc();
    9. }
    10. private:
    11. xyz(){ xyz::_instances << this; ref = 0; };
    12. static QList<xyz*> _instances;
    13. void inc(){ ref++; }
    14. int ref;
    15. };
    To copy to clipboard, switch view to plain text mode 

    This makes sure you can't create an object on stack and stores all instances of the class in the class itself.

    Edit: In the destructor the pointer in the list should be zeroed and a check for null pointer should be made before dereferencing it. I'll adjust the code...
    Last edited by wysota; 28th January 2006 at 12:38.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.