Problem copying objects.

E

esuvs81

Hi, imagine I have a simple hierarchy with three classes - 'Base',
'Derived1', and Derived2. Base is an absrtact class and the two Derived
classes are concrete. Imagine at some point in my code I have a pointer
p1 of type 'Base' which actually points to an instance of one of the
derived objects. I would like to create a new pointer p2, also of type
'Base', which points to a copy of whatever the first one pointed to.
That is, I would like to copy the object at p1 without knowing it's
exact type. Is that possible?

My first attempt was something like:

Base* p2 = new Base(*p1);

But this didn't make sense because i was trying to instanciate an
abstract class. Maybe i actually need to know the exact type? Another
approach I thought of was to give each class a virtual function called
'copy' which would duplicate the object and return a pointer to the
duplicate. Does this make sense or is there a better way?

Thanks for any help,

David
 
J

Jakob Bieling

Hi, imagine I have a simple hierarchy with three classes - 'Base',
'Derived1', and Derived2. Base is an absrtact class and the two
Derived classes are concrete. Imagine at some point in my code I have
a pointer p1 of type 'Base' which actually points to an instance of
one of the derived objects. I would like to create a new pointer p2,
also of type 'Base', which points to a copy of whatever the first one
pointed to. That is, I would like to copy the object at p1 without
knowing it's exact type. Is that possible?
approach I thought of was to give each class a virtual function called
'copy' which would duplicate the object and return a pointer to the
duplicate. Does this make sense or is there a better way?

Exactly, this is known as a 'virtual constructor'. Personally, I
find the name a bit misleading, as it does not have anything to do with
making the _constructor_ of the object virtual (because this is
impossible). Instead, you use a regular virtual function, as you
described, which will return a copy of the appropriate type. This is how
it works:

class base
{
public:
virtual base* clone () const = 0;
};

class derived1
{
public
virtual derived1* clone () const { return new derived1; }
};

class derived2
{
public
virtual derived2* clone () const { return new derived2; }
};

int main ()
{
base* b1 = new derived1;
base* b2 = b1->clone ();
};

hth
 
E

esuvs81

Jakob said:
Exactly, this is known as a 'virtual constructor'. Personally, I
find the name a bit misleading, as it does not have anything to do with
making the _constructor_ of the object virtual (because this is
impossible). Instead, you use a regular virtual function, as you
described, which will return a copy of the appropriate type. This is how
it works:

class base
{
public:
virtual base* clone () const = 0;
};

class derived1
{
public
virtual derived1* clone () const { return new derived1; }
};

class derived2
{
public
virtual derived2* clone () const { return new derived2; }
};

int main ()
{
base* b1 = new derived1;
base* b2 = b1->clone ();
};

hth

Great, thanks. I'll try that approach. I figured it would be a fairly
common problem and there would be some kind of design pattern for it
but didn't know what it was called. Thanks again.

David
 
T

Thomas J. Gritzan

Jakob said:
class base
{
public:
virtual base* clone () const = 0;
};

class derived1
{
public
virtual derived1* clone () const { return new derived1; }

virtual derived1* clone () const { return new derived1(*this); }

You want to clone here, don't you?
};

class derived2
{
public
virtual derived2* clone () const { return new derived2; }

virtual derived2* clone () const { return new derived2(*this); }
 
J

Jakob Bieling

Thomas J. Gritzan said:
Jakob Bieling schrieb:

virtual derived1* clone () const { return new derived1(*this); }

You want to clone here, don't you?


Right, but I take it it's a matter of preference if you want to
create a new object by cloning the type (which I usually do) or by
cloning the actual object (which is what you are doing).

regards
 

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,774
Messages
2,569,596
Members
45,127
Latest member
CyberDefense
Top