Results 1 to 9 of 9

Thread: Basic C++ doubt

  1. #1
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Basic C++ doubt

    Hi,

    Can some one please explain the function FredPtr& operator= (const FredPtr& p) int the following lines of code ?

    Qt Code:
    1. class FredPtr;
    2.  
    3. class Fred {
    4. public:
    5. Fred() : count_(0) /*...*/ { } // All ctors set count_ to 0 !
    6. ...
    7. private:
    8. friend class FredPtr; // A friend class
    9. unsigned count_;
    10. // count_ must be initialized to 0 by all constructors
    11. // count_ is the number of FredPtr objects that point at this
    12. };
    13.  
    14. class FredPtr {
    15. public:
    16. Fred* operator-> () { return p_; }
    17. Fred& operator* () { return *p_; }
    18. FredPtr(Fred* p) : p_(p) { ++p_->count_; } // p must not be NULL
    19. ~FredPtr() { if (--p_->count_ == 0) delete p_; }
    20. FredPtr(const FredPtr& p) : p_(p.p_) { ++p_->count_; }
    21. FredPtr& operator= (const FredPtr& p)
    22. { // DO NOT CHANGE THE ORDER OF THESE STATEMENTS!
    23. // (This order properly handles self-assignment)
    24. // (This order also properly handles recursion, e.g., if a Fred contains FredPtrs)
    25. Fred* const old = p_;//const pointer to a const Fred
    26. p_ = p.p_;
    27. ++p_->count_;
    28.  
    29. //Will -- operator not change the count ? But Fred is const ???
    30. if (--old->count_ == 0) delete old;
    31. return *this;
    32. }
    33. private:
    34. Fred* p_; // p_ is never NULL
    35. };
    To copy to clipboard, switch view to plain text mode 

    Thanks a lot.

  2. #2
    Join Date
    Jan 2006
    Location
    Alingsås, Sweden
    Posts
    437
    Thanks
    3
    Thanked 39 Times in 39 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Basic C++ doubt

    If several FredPtr (Fred pointers) point to the same Fred object, a reference counter in the Fred object is increased. If the FredPtr pointed at another Fred object that object's count is decreased (as the pointer stops pointing to it, it has one less reference). If this was the last pointer pointing to it, it is deleted.

    This provides some sort of automatic memory management as long as FredPtr is used instead of Fred*.

  3. #3
    Join Date
    Jan 2006
    Location
    Kerala
    Posts
    371
    Thanks
    76
    Thanked 37 Times in 32 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Basic C++ doubt

    This Qt Article will help you understand better -the reference counting concept

    http://doc.trolltech.com/qq/qq02-dat...ith-class.html
    We can't solve problems by using the same kind of thinking we used when we created them

  4. #4
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Basic C++ doubt

    Yes, I understand the functionality. But I did not understand the function
    FredPtr& operator= (const FredPtr& p)

    and why the code should be written in that particular order ?

    Thanks a lot for explaining

  5. #5
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Basic C++ doubt

    Can someone please explain ? I'll really be greatful to them.

    Thanks a lot for your time.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    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: Basic C++ doubt

    Qt Code:
    1. Fred* const old = p_;
    To copy to clipboard, switch view to plain text mode 
    This creates a temporary pointer pointing to the old data pointer of the object.

    Qt Code:
    1. p_ = p.p_;
    To copy to clipboard, switch view to plain text mode 
    This assigns a new pointer to the object (note that if p==this, it assigns a pointer which is already there).

    Qt Code:
    1. ++p_->count_;
    To copy to clipboard, switch view to plain text mode 
    This increases the reference count for the current (new) object data.

    Qt Code:
    1. if (--old->count_ == 0) delete old;
    To copy to clipboard, switch view to plain text mode 
    This decreases the reference count for the old pointer.

    The order (of the last two statements) is very important, let's see what happens if you change it and try to do:
    Qt Code:
    1. FredPtr p1;
    2. p1 = p1;
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. Fred* const old = p_;
    2. p_ = p.p_;
    3. if (--old->count_ == 0) delete old;
    4. ++p_->count_;
    To copy to clipboard, switch view to plain text mode 
    1. You create the "old" pointer pointing to p1.p_
    2. You assign p1.p_ to p1.p_
    3. You decrease the reference count of p1.p_. Now if the reference count prior to performing that operation was "1", it will now be decreased and the pointer will be freed and you'll lose your data.
    4. The pointer count will try to be increased, but the pointer is already invalid as you just deleted it and you get a pretty segfault.

    If you first increase and then decrease the reference count, you make sure that the ref count of the object assigned to the current object will never reach 0 and will not cause the pointer to be deleted by accident.

  7. The following 2 users say thank you to wysota for this useful post:

    munna (30th November 2006), sunil.thaha (1st December 2006)

  8. #7
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Basic C++ doubt

    It might be a very silly doubt, but please bear with me.

    Now let us consider the following code.

    Qt Code:
    1. class Fred {
    2. public:
    3. static FredPtr create();
    4. static FredPtr create(int i, int j);
    5. ...
    6. private:
    7. Fred();
    8. Fred(int i, int j);
    9. ...
    10. };
    11.  
    12. class FredPtr { /* ... */ };//the code above.
    13.  
    14. inline FredPtr Fred::create() { return new Fred(); }//Isn't the return type wrong ?
    15. inline FredPtr Fred::create(int i, int j) { return new Fred(i,j); }
    To copy to clipboard, switch view to plain text mode 

    and the code here

    Qt Code:
    1. FredPtr ptr1(Fred::create());
    2. FredPtr ptr2 = ptr1;//What will happen here ?
    To copy to clipboard, switch view to plain text mode 

    Can you please explain how count will change in this case ?

    Another doubt

    Fred* const old = p_; means old is a const pointer to a const Fred. Then how can it be changed in the statement "if (--old->count_ == 0)" ?

    Thanks a lot.
    Last edited by munna; 30th November 2006 at 14:33. Reason: updated contents

  9. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Basic C++ doubt

    Quote Originally Posted by munna View Post
    inline FredPtr Fred::create() { return new Fred(); }//Isn't the return type wrong ?
    No, because there's FredPtr::FredPtr(Fred* p) constructor[1] that will be used to convert Fred* to FredPtr.

    Quote Originally Posted by munna View Post
    FredPtr ptr1(Fred::create());
    FredPtr ptr2 = ptr1;//What will happen here ?
    Both FredPtrs will point to the same Fred object and the reference count will be 2.

    Quote Originally Posted by munna View Post
    Fred* const old = p_; means old is a const pointer to a const Fred.
    No, it's a constant pointer to a non-constant object.

    [1] and it doesn't have "explicit" keyword in front of it.

  10. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    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: Basic C++ doubt

    I think it's also worth to note that in this situation:
    Qt Code:
    1. FredPtr ptr2 = ptr1;
    To copy to clipboard, switch view to plain text mode 
    the compiler will use a copy constructor (line 20 of the listing in the first post) and not the assignment operator (lines 21-32 of the same listing).

Similar Threads

  1. Basic Qt4 tutorial needed
    By pthomas in forum Qt Programming
    Replies: 26
    Last Post: 16th October 2006, 15:11
  2. Replies: 5
    Last Post: 4th August 2006, 10:12
  3. Basic C++ doubts
    By munna in forum General Programming
    Replies: 3
    Last Post: 6th April 2006, 16:23
  4. Need Basic html Browser
    By awalesminfo in forum Newbie
    Replies: 6
    Last Post: 21st March 2006, 17:14
  5. Basic question on new and delete
    By jcr in forum General Programming
    Replies: 25
    Last Post: 14th February 2006, 15:09

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.