Re: Smart pointer referencing its owner
On Mar 7, 7:03 am, Pavel Minaev <int...@gmail.com> wrote:
....
I see how that works for auto_ptr now, but I'm still not so sure about
shared_ptr. Again, consider the following (simplified, not thread-
safe, not deleter & weak_ptr aware, etc - I don't think all these are
relevant here) implementation of reset() for it:
T* ptr;
int* refcountptr;
void reset(T* newptr = 0) {
if (this->refcountptr && --*this->refcountptr == 0) {
delete this->ptr;
delete this->refcountptr;
}
if (this->ptr = newptr) {
this->refcountptr = new int(1);
} else {
this->refcountptr = 0;
}
}
Broken.
What does the Standard say in this case?
template<class Y> void reset( Y* p );
Effects: Equivalent to shared_ptr(p).swap(*this).
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]