new Derived from Base pointer

Discussion in 'C++' started by bytebro, Mar 18, 2008.

  1. bytebro

    bytebro Guest

    I have a function which is passed a pointer to a Base object, and I
    would like to create a copy of the Derived object who's address was
    passed in. I was hoping I could do something like this:

    class Thing
    {
    private:
    Base* ptr_;
    Base* copyptr_

    public:
    Thing(Base* param) : ptr_(param), copyptr_(new Base(*param) {}
    }

    But of course this simply tries to create a new Base object which will
    fail if Base has any pure virtual functions. Is there some way of
    saying 'make me a copy of whatever was _really_ pointed to by the
    passed-in pointer'?!
     
    bytebro, Mar 18, 2008
    #1
    1. Advertising

  2. bytebro

    Guest

    On Mar 18, 3:54 am, bytebro <> wrote:
    > I have a function which is passed a pointer to a Base object, and I
    > would like to create a copy of the Derived object who's address was
    > passed in. I was hoping I could do something like this:
    >
    > class Thing
    > {
    > private:
    > Base* ptr_;
    > Base* copyptr_
    >
    > public:
    > Thing(Base* param) : ptr_(param), copyptr_(new Base(*param) {}
    >
    > }
    >
    > But of course this simply tries to create a new Base object which will
    > fail if Base has any pure virtual functions. Is there some way of
    > saying 'make me a copy of whatever was _really_ pointed to by the
    > passed-in pointer'?!


    You could use RTTI and a bit if-else to determine the type of Derived
    param points to, but what type would you make copyptr_?

    What exactly are you trying to accomplish here?
     
    , Mar 18, 2008
    #2
    1. Advertising

  3. bytebro

    Ian Collins Guest

    bytebro wrote:
    >
    > But of course this simply tries to create a new Base object which will
    > fail if Base has any pure virtual functions. Is there some way of
    > saying 'make me a copy of whatever was _really_ pointed to by the
    > passed-in pointer'?!


    Provide a virtual clone() method that returns a copy of the object.

    --
    Ian Collins.
     
    Ian Collins, Mar 18, 2008
    #3
  4. bytebro

    Paavo Helde Guest

    bytebro <> wrote in news:c0c51a0a-bdb5-4180-83dd-
    :

    >
    > I have a function which is passed a pointer to a Base object, and I
    > would like to create a copy of the Derived object who's address was
    > passed in. I was hoping I could do something like this:
    >


    This is usually done by adding a virtual pure Clone() function to
    the Base class, which each instantiable class in the hierarchy has to
    override. The function itself is usually very simple:

    class Derived {
    virtual Base* Clone() const {return new Derived(*this);}
    ....

    hth
    Paavo
     
    Paavo Helde, Mar 18, 2008
    #4
  5. bytebro

    bytebro Guest

    On 18 Mar, 09:52, Paavo Helde <> wrote:
    > bytebro <> wrote in news:c0c51a0a-bdb5-4180-83dd-
    > :
    >
    > > I have a function which is passed a pointer to a Base object, and I
    > > would like to create a copy of the Derived object who's address was
    > > passed in. I was hoping I could do something like this:

    >
    > This is usually done by adding a virtual pure Clone() function to
    > the Base class, which each instantiable class in the hierarchy has to
    > override. The function itself is usually very simple:


    That was exactly what I was looking for - thanks. And having then
    gone away and read up on it, I now understand the 'virtual constructor
    idiom', of which this is an example.
     
    bytebro, Mar 18, 2008
    #5
  6. bytebro

    Kai-Uwe Bux Guest

    bytebro wrote:

    >
    > I have a function which is passed a pointer to a Base object, and I
    > would like to create a copy of the Derived object who's address was
    > passed in. I was hoping I could do something like this:
    >
    > class Thing
    > {
    > private:
    > Base* ptr_;
    > Base* copyptr_
    >
    > public:
    > Thing(Base* param) : ptr_(param), copyptr_(new Base(*param) {}
    > }
    >
    > But of course this simply tries to create a new Base object which will
    > fail if Base has any pure virtual functions. Is there some way of
    > saying 'make me a copy of whatever was _really_ pointed to by the
    > passed-in pointer'?!


    Depending on what you are trying to accomplish, a smart pointer with
    deep-copy semantics may be of use. If you are interested, search the
    archives for copy_ptr. There are several implementations around.


    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Mar 18, 2008
    #6
  7. bytebro

    Harsh Puri Guest

    Hi,

    Yes there is a way. You need to define a virtual copy constructor,
    which will be like a Clone() function in your base class, ideally a
    pure virtual.

    This should be overridden by the derived classes to call their
    specific copy construtor and return the pointer. The signature would
    be:

    class Base{

    public:
    Base* Clone() = 0;
    };

    HTH
    Harsh

    On Mar 18, 8:54 am, bytebro <> wrote:
    > I have a function which is passed a pointer to a Base object, and I
    > would like to create a copy of the Derived object who's address was
    > passed in.  I was hoping I could do something like this:
    >
    > class Thing
    > {
    > private:
    >   Base* ptr_;
    >   Base* copyptr_
    >
    > public:
    >   Thing(Base* param) : ptr_(param), copyptr_(new Base(*param) {}
    >
    > }
    >
    > But of course this simply tries to create a new Base object which will
    > fail if Base has any pure virtual functions.  Is there some way of
    > saying 'make me a copy of whatever was _really_ pointed to by the
    > passed-in pointer'?!
     
    Harsh Puri, Mar 19, 2008
    #7
    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.

Share This Page