Results 1 to 9 of 9

Thread: Objects and members

  1. #1
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Objects and members

    Hi,
    I have a class:
    Qt Code:
    1. class myClass {
    2. int number;
    3. void incrNumber() {number++; }
    4. void something();
    5. };
    6. myclass c[3];
    7.  
    8. myClass::something() {
    9. //here i'd like make something such as myClass[3].incrNumber();
    10. }
    To copy to clipboard, switch view to plain text mode 
    Is there a way to modify the state of an instance of object only (and only one) from a method of myClass?
    Thanks.
    Regards

  2. #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.

  3. #3
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: Objects and members

    thanks...Is there only this way? Is what I want to do a thing to avoid?
    Thanks.
    Regards

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    85
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Objects and members

    yop, your code will not work:
    Qt Code:
    1. myClass::something() {
    2. //here i'd like make something such as myClass[3].incrNumber();
    3. if(index==3)
    4. incrNumber();
    5. }
    To copy to clipboard, switch view to plain text mode 
    Here you check index == 3, but index is a static member. When there are exactly three instances any call to something will call incrNumber.

    mickey, you need here another mechanism: One static member for the current count of objects and some kind of management which instances exist (use as an ID their adresses or another unique ID) plus a class local member with the ID if you do not use the address. The real problem comes now - which is instance number 3? Imagine you created 5 instances and then delete (maybe they only left scope) instances 0 and 3. What now? In my appoach you could ask the static instance management but how should it decide which instance is number 3?
    This gets even more complicated if you have threads.


    Can you tell us what you need or want to do in more detail? It seems to me, that this approach is somewhat difficult.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 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.

  6. #6
    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

    Quote Originally Posted by Codepoet
    Here you check index == 3, but index is a static member. When there are exactly three instances any call to something will call incrNumber.
    Yeap you're right I was thinking of something to count the instances of the class and this was the natural way, but unsusable... What about this one?
    Qt Code:
    1. class myClass {
    2. public:
    3. myClass():perInstanceIndex(allClassesIndex){++allClassesIndex;}
    4. myClass(const myClass&):perInstanceIndex(allClassesIndex){++allClassesIndex;}
    5. ~myClass(){--allClassesIndex;}
    6. int number;
    7. void incrNumber() {number++; }
    8. void something();
    9.  
    10. private:
    11. static size_t allClassesIndex;
    12. size_t perInstanceIndex;
    13. };
    14.  
    15. size_t myClass::allClassesIndex=0;
    16.  
    17. myClass::something() {
    18. //here i'd like make something such as myClass[3].incrNumber();
    19. if(perInstanceIndex==3)
    20. incrNumber();
    21. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by yop; 30th January 2006 at 10:10.

  7. #7
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    85
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Objects and members

    @yoy: Somewhat better but the destructor will "kill" you You could end up with several instances having the same ID. I like wysota's solution but that will only work for "small" numbers of long living objects.

    @mickey: can you write us more details about what you want to do or did we already solve your problem?

  8. #8
    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

    Quote Originally Posted by Codepoet
    @yoy: Somewhat better but the destructor will "kill" you You could end up with several instances having the same ID. I like wysota's solution but that will only work for "small" numbers of long living objects.
    True again, I 'll give up on this

  9. #9
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: Objects and members

    hi, I'm sorry for not fast reply.
    I have 3 instances (a,b,c) of MyWidget class. I'd like a function of widget 'a' that change the state of 'b' and 'c'
    I can use a variable index++ and an if (index==1) then....but after recognize the widget 'a', I must call a particular function member of widget 'b' and 'c'.
    (but using index++: Are the order of the constructors the same every time???).
    Regards

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.