This Qt Article will help you understand better -the reference counting concept
http://doc.trolltech.com/qq/qq02-dat...ith-class.html
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
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
Mithin
www.mithin.in
Can someone please explain ? I'll really be greatful to them.
Thanks a lot for your time.
Mithin
www.mithin.in
This creates a temporary pointer pointing to the old data pointer of the object.
This assigns a new pointer to the object (note that if p==this, it assigns a pointer which is already there).
This increases the reference count for the current (new) object data.
This decreases the reference count for the old pointer.Qt Code:
if (--old->count_ == 0) delete old;To copy to clipboard, switch view to plain text mode
The order (of the last two statements) is very important, let's see what happens if you change it and try to do:
1. You create the "old" pointer pointing to p1.p_Qt Code:
Fred* const old = p_; p_ = p.p_; if (--old->count_ == 0) delete old; ++p_->count_;To copy to clipboard, switch view to plain text mode
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.
munna (30th November 2006), sunil.thaha (1st December 2006)
It might be a very silly doubt, but please bear with me.
Now let us consider the following code.
Qt Code:
class Fred { public: static FredPtr create(); static FredPtr create(int i, int j); ... private: Fred(); Fred(int i, int j); ... }; class FredPtr { /* ... */ };//the code above. inline FredPtr Fred::create() { return new Fred(); }//Isn't the return type wrong ? 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:
FredPtr ptr1(Fred::create()); 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 15:33. Reason: updated contents
Mithin
www.mithin.in
No, because there's FredPtr::FredPtr(Fred* p) constructor[1] that will be used to convert Fred* to FredPtr.
Both FredPtrs will point to the same Fred object and the reference count will be 2.
No, it's a constant pointer to a non-constant object.
[1] and it doesn't have "explicit" keyword in front of it.
I think it's also worth to note that in this situation:
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).
Bookmarks