Copy constructor for a class that contains a pointer to a base classtype (newbie)

  • Thread starter =?ISO-8859-1?Q?Szabolcs_Horv=E1t?=
  • Start date
?

=?ISO-8859-1?Q?Szabolcs_Horv=E1t?=

This is a newbie question. Suppose I have a class A (see the attached
code) which contains a pointer that can point to an object of either
type D1 or D2. How do I write a copy constructor for A, so that the
object that it references also gets copied (I don't know whether it is a
D1 or D2 object)?

Thank you for your answers in advance,
Szabolcs

----

class B {
public:
virtual void print() = 0;
};

class D1 : public B {
int i;
public:
void print() { cout << i; }
};

class D2 : public B {
double x;
public:
void print() { cout << x; }
};

class A {
B *b;
};
 
D

David Harmon

On Fri, 27 Oct 2006 21:00:34 +0200 in comp.lang.c++, Szabolcs Horvát
This is a newbie question. Suppose I have a class A (see the attached
code) which contains a pointer that can point to an object of either
type D1 or D2. How do I write a copy constructor for A, so that the
object that it references also gets copied

As your example shows, the Object-Oriented way for a pointer to
point to different classes is for them to be derived classes of a
common base. In that case, one answer is for the base to declare
virtual B* clone() = 0;
and all the derived classes to implement that virtual function and
each class return a new copy object of its own type. Your copy
constructor calls the virtual b->clone() and gets the new copy that
it needs without concern about how many derived classes there are.
 
?

=?ISO-8859-1?Q?Szabolcs_Horv=E1t?=

David said:
In that case, one answer is for the base to declare
virtual B* clone() = 0;
and all the derived classes to implement that virtual function and
each class return a new copy object of its own type. Your copy
constructor calls the virtual b->clone() and gets the new copy that
it needs without concern about how many derived classes there are.

Thanks! This solves my problem, but I thought that there is a better
solution for this. What if I cannot change B, D1 and D2 (e.g. they are
part of a library)?

Szabolcs

P.S.: Sorry for the duplicate post. First I tried to post using Google
Groups, but I got an 500 Server Error. It seems that the message has
been sent, but it took more than 40 minutes for it to arrive.
 
K

Kai-Uwe Bux

Szabolcs said:
Thanks! This solves my problem, but I thought that there is a better
solution for this. What if I cannot change B, D1 and D2 (e.g. they are
part of a library)?

There are smart pointer templates with deep copy semantics that will help
you. Search the archives of this news group for copy_ptr<> or clone_ptr<>.


Best

Kai-Uwe Bux
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top