Inheritance question: best wrapper for copying base pointer?

M

mrstephengross

Hi folks... I've got a simple inheritance system (base class & 2
derived classes). In essence it looks like this:

class Base { public:
virtual ~Base() { };
virtual void required_function() = 0; };

class Derived1 : public Base { public:
virtual void required_function() { }
int x;};

class Derived2 : public Base { public:
virtual void required_function() { }
int y;};

I want to store Base*'s in another struct, like so:

struct SomeStruct { Base * base; };

I want to write a copy constructor for SomeStruct so that the Base* is
deep-copied. I know there are a bunch of pointer wrappers out there for
doing so (auto_ptr, etc.). What's the simplest one to use for this
purpose?

Thanks,
--Steve ([email protected])
 
K

Kai-Uwe Bux

mrstephengross said:
Hi folks... I've got a simple inheritance system (base class & 2
derived classes). In essence it looks like this:

class Base { public:
virtual ~Base() { };
virtual void required_function() = 0; };

class Derived1 : public Base { public:
virtual void required_function() { }
int x;};

class Derived2 : public Base { public:
virtual void required_function() { }
int y;};

I want to store Base*'s in another struct, like so:

struct SomeStruct { Base * base; };

I want to write a copy constructor for SomeStruct so that the Base* is
deep-copied. I know there are a bunch of pointer wrappers out there for
doing so (auto_ptr, etc.). What's the simplest one to use for this
purpose?

Google for copy_ptr or clone_ptr in this group and in
comp.lang.c++.moderated. You will find that Axter and myself have put code
to illustrate how those pointers can be implemented. You can find more
refined versions of his on www.axter.com.


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
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top