auto_ptr and copy ctr question

Discussion in 'C++' started by Soumen, May 29, 2008.

  1. Soumen

    Soumen Guest

    If I've class with one of data members of auto_ptr type, how should I
    write copy ctr or copy assignment operator if I don't intend to allow
    transfer of ownership happen? Declaring that member const could be one
    option I guess. Clarify me if this understanding is wrong. Do I need
    to disable these (copy ctr and assignment operator) in such cases?
    What happens when I don't write one and use the compiler generated
    one? Does transfer of ownership still happens or it's same as
    declaring member as const? Looks like disabling these two are best
    design decision.

    Regards,
    ~ Soumen
    Soumen, May 29, 2008
    #1
    1. Advertising

  2. Soumen

    Soumen Guest

    On May 29, 6:24 pm, Victor Bazarov <> wrote:
    > Soumen wrote:
    > > If I've class with one of data members of auto_ptr type, how should I
    > > write copy ctr or copy assignment operator if I don't intend to allow
    > > transfer of ownership happen? Declaring that member const could be one
    > > option I guess. Clarify me if this understanding is wrong. Do I need
    > > to disable these (copy ctr and assignment operator) in such cases?
    > > What happens when I don't write one and use the compiler generated
    > > one? Does transfer of ownership still happens or it's same as
    > > declaring member as const? Looks like disabling these two are best
    > > design decision.

    >
    > If you don't provide the "move semantics" offered by the auto_ptr
    > itself, you could do two things. One, as you mentioned, is to prohibit
    > the copying altogether. That's a bit harsh to me, but if your design
    > can endure that, it's the simplest of the two. The other is to provide
    > copying of the owned object by copy-creating a new one in case of the
    > constructor and copy-assigning the pointed-to item in case of the
    > assignment:
    >
    > class Has_auto_ptr {
    > std::auto_ptr<sometype> myPtr;
    > public:
    > ...
    > Has_auto_ptr(Has_auto_ptr const& other) :
    > myPtr(new sometype(*other.myPtr)) {}
    > Has_auto_ptr& operator =(Has_auto_ptr const& other) {
    > *myPtr = *other.myPtr;
    > return *this;
    > }
    > };
    >
    > Disclaimer: the code is unchecked, provided for illustration purposes only.
    >
    > V
    > --
    > Please remove capital 'A's when replying by e-mail
    > I do not respond to top-posted replies, please don't ask


    Thanks. Yes, deep copy is always an option.

    The reason for not allowing transfer of ownership is original
    object's data can potentially become invalid if copied object
    goes out of scope.

    Is there any better standard smart pointer which does some sort
    of ref counting such that I can allow copy of pointed address
    and still not bother of freeing the resource or pointer becoming
    invalid?

    Regards,
    ~ Soumen
    Soumen, May 29, 2008
    #2
    1. Advertising

  3. Soumen

    James Kanze Guest

    On May 29, 3:16 pm, Soumen <> wrote:
    > If I've class with one of data members of auto_ptr type, how
    > should I write copy ctr or copy assignment operator if I don't
    > intend to allow transfer of ownership happen? Declaring that
    > member const could be one option I guess. Clarify me if this
    > understanding is wrong. Do I need to disable these (copy ctr
    > and assignment operator) in such cases? What happens when I
    > don't write one and use the compiler generated one? Does
    > transfer of ownership still happens or it's same as declaring
    > member as const? Looks like disabling these two are best
    > design decision.


    It depends on the desired semantics. If transfer of ownership
    is not desired, you have two choices: don't support copy (by
    declaring the copy constructor private, and not providing an
    implementation), or write a copy constructor which does a deep
    copy.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, May 29, 2008
    #3
  4. Soumen

    Triple-DES Guest

    On 29 Mai, 15:52, Soumen <> wrote:
    > Is there any better standard smart pointer which does some sort
    > of ref counting such that I can allow copy of pointed address
    > and still not bother of freeing the resource or pointer becoming
    > invalid?
    >
    > Regards,
    > ~ Soumen


    boost/tr1 :: shared_ptr is widely used, however it is not standard
    yet.

    DP
    Triple-DES, May 30, 2008
    #4
  5. Soumen

    Soumen Guest

    On May 30, 10:33 am, Triple-DES <> wrote:
    > On 29 Mai, 15:52, Soumen <> wrote:
    >
    > > Is there any better standard smart pointer which does some sort
    > > of ref counting such that I can allow copy of pointed address
    > > and still not bother of freeing the resource or pointer becoming
    > > invalid?

    >
    > > Regards,
    > > ~ Soumen

    >
    > boost/tr1 :: shared_ptr is widely used, however it is not standard
    > yet.
    >
    > DP


    Thanks. Though not part of standard, if I use boost::shared_ptr,
    boost::shared_array,
    boost::weak_ptr etc. properly, will the code be portable to other
    platform?

    Regards,
    ~ Soumen
    Soumen, May 30, 2008
    #5
  6. Soumen

    dizzy Guest

    Soumen wrote:

    > On May 30, 10:33 am, Triple-DES <> wrote:
    >> On 29 Mai, 15:52, Soumen <> wrote:
    >>
    >> > Is there any better standard smart pointer which does some sort
    >> > of ref counting such that I can allow copy of pointed address
    >> > and still not bother of freeing the resource or pointer becoming
    >> > invalid?

    >>
    >> > Regards,
    >> > ~ Soumen

    >>
    >> boost/tr1 :: shared_ptr is widely used, however it is not standard
    >> yet.
    >>
    >> DP

    >
    > Thanks. Though not part of standard, if I use boost::shared_ptr,
    > boost::shared_array,
    > boost::weak_ptr etc. properly, will the code be portable to other
    > platform?


    It will work on all the platforms that boost supports, which are plenty. It
    will work on some others too if we assume shared_ptr is using just a small
    part of the C++ features that boost requires in general.

    --
    Dizzy
    dizzy, May 30, 2008
    #6
  7. Soumen

    Soumen Guest

    On May 30, 1:54 pm, dizzy <> wrote:
    > Soumen wrote:
    > > On May 30, 10:33 am, Triple-DES <> wrote:
    > >> On 29 Mai, 15:52, Soumen <> wrote:

    >
    > >> > Is there any better standard smart pointer which does some sort
    > >> > of ref counting such that I can allow copy of pointed address
    > >> > and still not bother of freeing the resource or pointer becoming
    > >> > invalid?

    >
    > >> > Regards,
    > >> > ~ Soumen

    >
    > >> boost/tr1 :: shared_ptr is widely used, however it is not standard
    > >> yet.

    >
    > >> DP

    >
    > > Thanks. Though not part of standard, if I use boost::shared_ptr,
    > > boost::shared_array,
    > > boost::weak_ptr etc. properly, will the code be portable to other
    > > platform?

    >
    > It will work on all the platforms that boost supports, which are plenty. It
    > will work on some others too if we assume shared_ptr is using just a small
    > part of the C++ features that boost requires in general.
    >
    > --
    > Dizzy


    Thanks. Also, could you please give an idea about the overhead of this
    additional
    book-keeping on efficiency?

    Regards,
    ~ Soumen
    Soumen, Jun 2, 2008
    #7
  8. Soumen

    Soumen Guest

    On Jun 2, 6:01 pm, Victor Bazarov <> wrote:
    > Soumen wrote:
    > > [..]
    > > Thanks. Also, could you please give an idea about the overhead of this
    > > additional
    > > book-keeping on efficiency?

    >
    > Additional bookkeeping lowers the performance.
    >
    > Oh... Did you want numbers? Sorry, no numbers. Put it in, profile,
    > and if you find it to be the bottleneck, deal with it then and there,
    > not before even attempting to use it.
    >
    > V
    > --
    > Please remove capital 'A's when replying by e-mail
    > I do not respond to top-posted replies, please don't ask


    Thanks for reminding "Don't do pre-mature optimization".
    Soumen, Jun 2, 2008
    #8
    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. Arnt O. Kvannefoss
    Replies:
    6
    Views:
    493
    Arnt O. Kvannefoss
    Apr 8, 2005
  2. Siemel Naran

    auto_ptr<Derived> to auto_ptr<Base>

    Siemel Naran, Jan 10, 2005, in forum: C++
    Replies:
    2
    Views:
    1,533
    Dave Rahardja
    Jan 11, 2005
  3. jimgardener
    Replies:
    1
    Views:
    3,751
    jimgardener
    Dec 5, 2008
  4. Sousuke
    Replies:
    9
    Views:
    1,127
    Bart van Ingen Schenau
    Mar 16, 2010
  5. bob smith

    ctr++ or ++ctr

    bob smith, Aug 30, 2013, in forum: C++
    Replies:
    22
    Views:
    479
Loading...

Share This Page