471,319 Members | 1,587 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,319 software developers and data experts.

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

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.

Aug 20 '07 #1
3 3650
Hi!

ja************@gmail.com schrieb:
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
Aug 20 '07 #2
On Aug 20, 4:20 pm, jacek.dzied...@gmail.com wrote:
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-lit....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
Aug 20 '07 #3
On Aug 20, 1:41 pm, Frank Birbacher <bloodymir.c...@gmx.netwrote:
Hi!

jacek.dzied...@gmail.com schrieb:
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:
[...]

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

cheers,
- J.

Aug 20 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

42 posts views Thread by Edward Diener | last post: by
5 posts views Thread by Tony Johansson | last post: by
20 posts views Thread by verec | last post: by
4 posts views Thread by fperfect13 | last post: by
12 posts views Thread by Bob | last post: by
4 posts views Thread by jjkboswell | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.