new Derived from Base pointer

B

bytebro

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'?!
 
D

dave_mikesell

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?
 
I

Ian Collins

bytebro said:
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.
 
P

Paavo Helde

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
 
B

bytebro

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.
 
K

Kai-Uwe Bux

bytebro said:
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
 
H

Harsh Puri

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
 

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
474,262
Messages
2,571,052
Members
48,769
Latest member
Clifft

Latest Threads

Top