deep copy of a polymorphic object with only a base ptr?

J

jacek.dziedzic

Hello!

Suppose I have a class base, with virtual methods and a virtual
destructor and a bunch of classes,
derived1, derived2, ... which publicly derive from base. I then have a
pointer

base* foo;

which a complicated code allocates as one of derived's and sets up.
Later on I have polymorphic
access to the underlying object via this base pointer foo.

At one point I need to create a deep copy of the object behind foo,
i.e. I have

base* foo_copy; // how do I deep-copy (*foo) into here?

This needs to copy a derived class, but, since I only have a base
pointer I do not know how
to go on about it, the first obstacle being the typename needed in the
new-expression which
would be used to allocate memory for foo_copy.

I tried new typeof(*foo), but, as far as I see, it is not
polymorphic and new'ed me a base class.

Sure, I could try dynamic_cast<>'ing foo to every possible derived
and see which one does
not return NULL (or is it bad_cast?) but that would involve a nasty
switch, wouldn't it?

So, the bottom line is -- how do I deep copy an object of one of the
derived classes, whilst
only having the base class pointer to the instance I want to copy?

Also, both the base classes and the derived classes do not have
(user-defined) copy ctors,
since they are almost-POD and I'm satisfied with the memberwise copy
construction. Is this
compiler-generated copy ctor safe to use here?

TIA,
- J.
 
F

Frank Birbacher

Hi!

So, the bottom line is -- how do I deep copy an object of one of the
derived classes, whilst
only having the base class pointer to the instance I want to copy?

The standard solution is to extend the base class interface:

class Base
{
protected:
virtual Base* doClone() const =0;
public:
Base* clone() const { return doClone(); }
};

class Dev1 : public Base
{
protected:
virtual Dev1* doClone() const
{ return new Dev1(*this); }
public:
Dev1* clone() const { return doClone(); }
};

Then you can "clone" objects:
Base* const b1 = new Dev1;
Base* const b2 = b1->clone();
Also, both the base classes and the derived classes do not have
(user-defined) copy ctors,
since they are almost-POD and I'm satisfied with the memberwise copy
construction. Is this
compiler-generated copy ctor safe to use here?

The generated copy ctor is defined as memberwise copy. It should not
only be fine in your case, but it is usually a good choice.

HTH,
Frank
 
N

Neelesh Bodas

Hello!

Suppose I have a class base, with virtual methods and a virtual
destructor and a bunch of classes,
derived1, derived2, ... which publicly derive from base. I then have a
pointer

base* foo;

which a complicated code allocates as one of derived's and sets up.
Later on I have polymorphic
access to the underlying object via this base pointer foo.

At one point I need to create a deep copy of the object behind foo,
i.e. I have

base* foo_copy; // how do I deep-copy (*foo) into here?

This needs to copy a derived class, but, since I only have a base
pointer I do not know how
to go on about it, the first obstacle being the typename needed in the
new-expression which
would be used to allocate memory for foo_copy.

You can define virtual clone() member function. Refer
http://www.parashift.com/c++-faq-lite/virtual-functions.html#faq-20.8
for details.
I tried new typeof(*foo), but, as far as I see, it is not
polymorphic and new'ed me a base class.

Sure, I could try dynamic_cast<>'ing foo to every possible derived
and see which one does
not return NULL (or is it bad_cast?) but that would involve a nasty
switch, wouldn't it?

So, the bottom line is -- how do I deep copy an object of one of the
derived classes, whilst
only having the base class pointer to the instance I want to copy?

Also, both the base classes and the derived classes do not have
(user-defined) copy ctors,
since they are almost-POD and I'm satisfied with the memberwise copy
construction. Is this
compiler-generated copy ctor safe to use here?

If you are "satisfied" with memberwise copy, then it is probably the
correct choice.

-N
 
J

jacek.dziedzic

Hi!



The standard solution is to extend the base class interface:

[...]

Thank you very much, (and to Neelash Bodas also) for the solution!

cheers,
- J.
 

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

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top