How to copy derived class from a base pointer

Discussion in 'C++' started by cindypwl@lycos.com, Mar 18, 2006.

  1. Guest

    This is what I have

    class base{public:virtual ~base();};
    class derived1 : public base {};
    class derived2 : public base {};

    class A
    {
    public:
    A(base *b_):b(b_) {
    }
    A(const A&a)
    {
    //What to do here *************
    }
    private:
    base *b;
    };

    I'm not sure how to write the copy constructor to make sure it makes
    the right copy of b.
    How would the copy constructor know which derived type to copy?
     
    , Mar 18, 2006
    #1
    1. Advertising

  2. Guest

    in the class base{public:virtual ~base();};
    you should add virtual clone() = 0;
    the things will be OK!
    cheer!
     
    , Mar 18, 2006
    #2
    1. Advertising

  3. chenchen Guest

    add a member function - baseclass copy constructor
     
    chenchen, Mar 18, 2006
    #3
  4. Maxim Yegorushkin, Mar 18, 2006
    #4
  5. Axter Guest

    wrote:
    > This is what I have
    >
    > class base{public:virtual ~base();};
    > class derived1 : public base {};
    > class derived2 : public base {};
    >
    > class A
    > {
    > public:
    > A(base *b_):b(b_) {
    > }
    > A(const A&a)
    > {
    > //What to do here *************
    > }
    > private:
    > base *b;
    > };
    >
    > I'm not sure how to write the copy constructor to make sure it makes
    > the right copy of b.
    > How would the copy constructor know which derived type to copy?


    You can use the following smart pointer:
    http://axter.com/smartptr

    The smart pointer in the above link has a default policy to clone
    (deep-copy) your derived type.
    Moreover, you don't need to create a clone function for your base
    class, because this smart pointer can automatically determine the
    derived type by using the type pass to the constructor.
    You would have to change your constructor to the following:
    class A
    {
    public:
    A(smart_ptr<base> b_):b(b_) {
    }
    A(const A&a):b(a.b)
    {
    //Now you don't have to do anything here
    }
    private:
    smart_ptr<base> b;
    };

    If your A class only has the b member, you don't even need a copy
    constructor for your A class, because the smart pointer will clone
    automatically.

    The smart_ptr is also more efficient and more flexible than the
    boost::shared_ptr, which doesn't clone.

    ---------------------------------------------------------------------------­-------------

    David Maisonave
    http://axter.com


    Top ten member of C++ Expert Exchange:
    http://www.experts-exchange.com/Cplusplus
    ---------------------------------------------------------------------------­-------------
     
    Axter, Mar 18, 2006
    #5
  6. Axter Guest

    Maxim Yegorushkin wrote:
    > wrote:
    >
    > []
    >
    > > I'm not sure how to write the copy constructor to make sure it makes
    > > the right copy of b.
    > > How would the copy constructor know which derived type to copy?

    >
    > http://www.parashift.com/c -faq-lite/virtual-functions.html#faq-20.8


    I recommend against using this method posted in the FAQ.
    With this method it's harder to detect slicing, which can occur if a
    derived derived type fails to implement the clone function.

    With the clone function method, the best you can do is to try to detect
    slicing when and if cloning occurs. If cloning never occurs during
    testing, the bug doesn't get detected until your customer reports it to
    you.

    With the default method used in the following smart_ptr, slicing can be
    detected on the smart pointer's constructor:
    http://axter.com/smartptr

    This makes it much more likely to be caught during testing, compare to
    the clone function method.
    More over, the default method used in the smart_ptr requires less
    maintenance than that of the clone function method.

    For more information, read the smart_ptr link.
     
    Axter, Mar 18, 2006
    #6
  7. Fraser Ross Guest

    Would this smart pointer be able to store STL exception objects somehow
    that are caught with a reference to the base?

    Fraser.


    *** Free account sponsored by SecureIX.com ***
    *** Encrypt your Internet usage with a free VPN account from http://www.SecureIX.com ***
     
    Fraser Ross, Mar 18, 2006
    #7
  8. Axter Guest

    Fraser Ross wrote:
    > Would this smart pointer be able to store STL exception objects somehow
    > that are caught with a reference to the base?
    >
    > Fraser.


    You don't store exceptions, so I'm not sure what exactly you're
    referring to.

    What ever exceptions handling you have with a raw pointer of the base
    type, you'll also have with the smart pointer of the base type.

    ---------------------------------------------------------------------------­-------------

    David Maisonave
    http://axter.com


    Top ten member of C++ Expert Exchange:
    http://www.experts-exchange.com/Cplusplus
    ---------------------------------------------------------------------------­-------------
     
    Axter, Mar 18, 2006
    #8
  9. Guest

    Axter wrote:
    > Maxim Yegorushkin wrote:
    > > wrote:

    > With the default method used in the following smart_ptr, slicing can be
    > detected on the smart pointer's constructor:
    > http://axter.com/smartptr


    I notice that link also has a sync_ptr class that automatically locks
    the smart pointer, and the smart_ptr class also has lock logic, but it
    uses a scope_lock class.

    Why doesn't the smart_ptr class have the same automatic lock interface
    as does the sync_ptr class?

    I have a multithreading application in which I think I can use this
    type of smart pointer, but the sync_ptr looks like a better choice.
    But it doesn't have reference counting.
     
    , Mar 19, 2006
    #9
  10. Axter Guest

    wrote:
    > Axter wrote:
    > > Maxim Yegorushkin wrote:
    > > > wrote:

    > > With the default method used in the following smart_ptr, slicing can be
    > > detected on the smart pointer's constructor:
    > > http://axter.com/smartptr

    >
    > I notice that link also has a sync_ptr class that automatically locks
    > the smart pointer, and the smart_ptr class also has lock logic, but it
    > uses a scope_lock class.
    >
    > Why doesn't the smart_ptr class have the same automatic lock interface
    > as does the sync_ptr class?
    >
    > I have a multithreading application in which I think I can use this
    > type of smart pointer, but the sync_ptr looks like a better choice.
    > But it doesn't have reference counting.


    I have future plans for adding all the features of the sync_ptr class
    to the smart_ptr class.
    It's rare to find a smart pointer using reference counting logic and
    synchronization logic together. So the first stage of adding
    synchronization logic to smart_ptr was just a proof of concept.
    Now that I'm certain it can be done, I can go ahead and add the other
    features.
     
    Axter, Mar 19, 2006
    #10
    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. Karl Heinz Buchegger
    Replies:
    3
    Views:
    544
    Karl Heinz Buchegger
    Aug 6, 2003
  2. tirath
    Replies:
    3
    Views:
    735
    Ivan Vecerina
    Oct 12, 2003
  3. Replies:
    1
    Views:
    425
    myork
    May 23, 2007
  4. Replies:
    1
    Views:
    413
    Victor Bazarov
    May 23, 2007
  5. , India
    Replies:
    8
    Views:
    1,016
    gwowen
    Aug 18, 2010
Loading...

Share This Page