By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,286 Members | 2,364 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,286 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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.