c++ faq 16.22 reference counting

Discussion in 'C++' started by Vladimir Jovic, Jul 21, 2010.

  1. 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?
    Vladimir Jovic, Jul 21, 2010
    #1
    1. Advertising

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

    --
    blog at <url: http://alfps.wordpress.com>
    Alf P. Steinbach /Usenet, Jul 21, 2010
    #2
    1. Advertising

  3. Vladimir Jovic

    Kai-Uwe Bux Guest

    Vladimir Jovic wrote:

    > 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
    Kai-Uwe Bux, Jul 21, 2010
    #3
  4. Vladimir Jovic

    Öö Tiib Guest

    Re: c++ faq 16.22 reference counting

    On 21 juuli, 23:07, Kai-Uwe Bux <> wrote:
    > Vladimir Jovic wrote:
    > > 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.


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

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

    > >     return *this;
    > > }
    Öö Tiib, Jul 21, 2010
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ash
    Replies:
    1
    Views:
    414
    SenderX
    Oct 24, 2003
  2. Kalle Rutanen

    Reference counting in C++

    Kalle Rutanen, May 7, 2005, in forum: C++
    Replies:
    0
    Views:
    560
    Kalle Rutanen
    May 7, 2005
  3. Tony Johansson

    reference counting

    Tony Johansson, May 22, 2005, in forum: C++
    Replies:
    4
    Views:
    369
    Andrew Koenig
    May 23, 2005
  4. mathieu
    Replies:
    8
    Views:
    509
    Juha Nieminen
    Aug 31, 2008
  5. edwardfredriks

    counting up instead of counting down

    edwardfredriks, Sep 6, 2005, in forum: Javascript
    Replies:
    6
    Views:
    199
    Dr John Stockton
    Sep 7, 2005
Loading...

Share This Page