473,218 Members | 1,869 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,218 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 3864
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

42
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same...
5
by: Tony Johansson | last post by:
Hello! I'm reading in a book about C++ and that is something that sound strange. It says "Pointers have reference-assignment semantics similar to those in Java. For example, after the...
20
by: verec | last post by:
One problem I've come accross in designing a specific version of auto_ptr is that I have to disntiguish between "polymorphic" arguments and "plain" ones, because the template has to, internally,...
4
by: fperfect13 | last post by:
Hi, I wanted to perform a deep copy of an array. Searching on google I ran into different opinions : C# Interview Questions (http://blogs.wwwcoder.com/tsvmadhav/archive/2005/04/08/2882.aspx)...
12
by: Bob | last post by:
Hi, 'Shadowed' properties are not polymorphic. (See thread 'Inheritance and late binding') They should be. Problem: Base class has read only property 'X'. Derived class must have read / write...
4
by: jjkboswell | last post by:
I have an XSD which I have generated a class from using the xsd.exe tool. My XSD contains complex types within it, so that the generated class has member variables which are of types that are also...
11
by: Nindi73 | last post by:
A few days a ago I posted my code for a deep copy pointer which doesn't require the pointee object to have a virtual copy constructor. I need help with checking that it was exception safe and...
3
by: raylopez99 | last post by:
The "C# Cookbook" (O'Reilly / Jay Hilyard), section 3.26, is on deep cloning versus shallow cloning. The scanned pages of this book are found here: http://www.sendspace.com/file/mjyocg (Word...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.