Results 1 to 9 of 9

Thread: Objects and members

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #2
    Join Date
    Jan 2006
    Location
    Athens - Greece
    Posts
    219
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Objects and members

    Qt Code:
    1. class myClass {
    2. myClass(){++index;}
    3. myClass(const myClass&){++index;}
    4. ~myClass(){--index;}
    5. int number;
    6. void incrNumber() {number++; }
    7. void something();
    8.  
    9. private:
    10. static size_t index;
    11. };
    12.  
    13. size_t myClass::index=0;
    14.  
    15. myClass::something() {
    16. //here i'd like make something such as myClass[3].incrNumber();
    17. if(index==3)
    18. incrNumber();
    19. }
    To copy to clipboard, switch view to plain text mode 

    You'll see some obvious things left out such as uninitialized number var in the ctor but you get the idea. You'll have to be extra carerfull on how many objects are also created behind your back as they will be indexed too. I did not create a copy constructor to avoid indexing compiler generated objects.
    [Edit]...witch is plain wrong as the destructor will decrease the index, I modified the source to also have a copy constructor.
    Last edited by yop; 28th January 2006 at 02:18.

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.