| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"" |
| Date: |
20 Aug 2007 06:20:57 AM |
| Object: |
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.
.
|
|
| User: "Frank Birbacher" |
|
| Title: Re: deep copy of a polymorphic object with only a base ptr? |
20 Aug 2007 06:41:00 AM |
|
|
Hi!
jacek.dziedzic@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
.
|
|
|
| User: "" |
|
| Title: Re: deep copy of a polymorphic object with only a base ptr? |
20 Aug 2007 07:02:01 AM |
|
|
On Aug 20, 1:41 pm, Frank Birbacher <bloodymir.c...@gmx.net> wrote:
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.
.
|
|
|
|
|
| User: "Neelesh Bodas" |
|
| Title: Re: deep copy of a polymorphic object with only a base ptr? |
20 Aug 2007 06:43:38 AM |
|
|
On Aug 20, 4:20 pm, 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-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
.
|
|
|
|

|
Related Articles |
|
|