c++ faq 16.22 reference counting

V

Vladimir Jovic

Hello,

Reading 16.22 in c++ faq, I wondered why the assign operator wasn't
implemented like this :

FredPtr& operator= (FredPtr const& p)
{
FredPtr tmp( p );

std::swap( tmp, *this );

return *this;
}

The effect should be the same. tmp goes out of scope, and old object is
destroyed (count is decreated, and old object deleted if count=0), and
new object is copy constructed, therefore the count is increased by 1.

Is this correct, or am I missing something?
 
A

Alf P. Steinbach /Usenet

* Vladimir Jovic, on 21.07.2010 11:35:
Reading 16.22 in c++ faq, I wondered why the assign operator wasn't
implemented like this :

FredPtr& operator= (FredPtr const& p)
{
FredPtr tmp( p );

std::swap( tmp, *this );

return *this;
}

I haven't looked at the FAQ, but the call to std::swap here would invoke
FredPtr::eek:perator=, recursively...

I think you meant something like

FredPtr& operator=( FredPtr const& p )
{
FredPtr temp( p );
swapWith( p );
return *this;
}

which can also be written as

FredPtr& operator=( FredPtr const& p )
{
FredPtr( p ).swapWith( *this );
return *this;
}

or alternatively as the short & sweet

FredPtr& operator=( FredPtr p )
{
swapWith( p );
return *this;
}

The effect should be the same. tmp goes out of scope, and old object is
destroyed (count is decreated, and old object deleted if count=0), and
new object is copy constructed, therefore the count is increased by 1.

Is this correct, or am I missing something?

Not sure. I'd guess the FAQ item is probably illustrating something else?


Cheers,

- Alf
 
K

Kai-Uwe Bux

Vladimir said:
Hello,

Reading 16.22 in c++ faq, I wondered why the assign operator wasn't
implemented like this :

FredPtr& operator= (FredPtr const& p)
{
FredPtr tmp( p );

std::swap( tmp, *this );

Here, you want to use a swap function provided by the FredPtr class:
std::swap() would rely on operator= triggering an infinite loop.
return *this;
}

The effect should be the same. tmp goes out of scope, and old object is
destroyed (count is decreated, and old object deleted if count=0), and
new object is copy constructed, therefore the count is increased by 1.

Is this correct, or am I missing something?

You are correct. I think, this particular FAQ item was written before the
copy-swap idiom became fashionable. The reference counting example nicely
illustrates one of the strong points of the copy-swap idiom: it separates
concerns into construction, destruction, and swapping internals.
Implementing the assignment operator from scratch makes one think about the
proper order of operations, and it is not so trivial to get it right.


Best

Kai-Uwe Bux
 
Ö

Öö Tiib

Here, you want to use a swap function provided by the FredPtr class:
std::swap() would rely on operator= triggering an infinite loop.

Or, you may simply use std::swap( tmp.p_, p_ );

It is because whole state of FredPtr is in that p_.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,816
Latest member
nipsseyhussle

Latest Threads

Top