Copying an object using base class pointers

M

Martin Magnusson

Hi!
I have a class with a private member which is a pointer to an abstract
class, which looks something like this:

class Agent
{
public:
void Step( Base* newB );

private:
Base* previousB;
};

For each time Step() is called, it should copy the contents of newB to
previousB, as the next time Step() is called, it needs to do stuff with
previousB. Now, how can I do that without knowing what derived class
newB actually is? Just copying the pointer won't work, since what it
points to may well change between calls. Can I use memcpy in some way,
or do I need to make a derived class of Agent which uses a "Derived
previousD;" instead of "Base* previousB;"?

/ martin
 
R

Ron Natalie

Martin Magnusson said:
. Can I use memcpy in some way,
or do I need to make a derived class of Agent which uses a "Derived
previousD;" instead of "Base* previousB;"?

If I understand what you want, you need a concept sometimes
referred to as a virtual constructor. I call it a "clone" function.
It involves a change to your objects.

class Base {
public:
virtual Base* clone() = 0;
//...
};

class DerivedA : public Base {
public:
Base* clone() { return new DerivedA(this); }
//...
};

class Agent {
public:
void Step(Base* newB) {
previousB = newB->clone();
};
private:
Base* previousB;
};
 
?

=?iso-8859-1?Q?Juli=E1n?= Albo

Martin Magnusson escribió:
For each time Step() is called, it should copy the contents of newB to
previousB, as the next time Step() is called, it needs to do stuff with
previousB. Now, how can I do that without knowing what derived class
newB actually is? Just copying the pointer won't work, since what it
points to may well change between calls. Can I use memcpy in some way,

Define a virtual function called clone or similar that returns a newed
copy of the object and implement it on all derived classes. This type of
functions are sometimes called "virtual constructors".

Regards.
 
P

paranoid?

Hi Martin,


Martin Magnusson said:
I have a class with a private member which is a pointer to an abstract
class, which looks something like this:

class Agent
{
public:
void Step( Base* newB );

private:
Base* previousB;
};

For each time Step() is called, it should copy the contents of newB to
previousB, as the next time Step() is called, it needs to do stuff with
previousB. Now, how can I do that without knowing what derived class
newB actually is?

You can't. At least not without limitations.

I propose a solution:

Have the base class define a virtual function whose intention is to copy a
source object into itself. Maybee like this:

void Assign(const Base* aSource);

Either impliment the functions in the base class, or make the base class
pure virtual. Then in subclasses polymorphically override the function to
perform the assignment of aSource to *this. Each implimenation will
(naturally) be different and may call inherited versions of the function to
help complete the job.

Have all subclasses define copy constrcutors (you should always define copy
constrcutors anyways):

Base(const Base* aSource);
Base(Base* aSource);

But the polymorphic Assign cannot be used alone to solve the problem as you
can probably already see... The trouble comes in if aSource isnt the exact
same type (but inherits from) the implimenting type. The implimenting type
may not (should not) be aware of the various inheriting sub-types. Even if
it was, it would be unable to destroy itself and recreate itself as the
correct subtype.

Delegate this job to the aggregating object (Agent). Thus - your Step()
function would first do a type check on the newB parameter and if it is the
same type as the previousB, simply call previousB->Assign(newB);

However - if the types are not exactly the same, then immediately destroy
the previousB, create a new instance of the exact same type of newB (and
pass newB to its copy constructors) then simply assign the new object to
previousB.

If you want to get really fancy here - you may wish to consider using a the
design pattern of a factory class to create objects in the scope of Step() -
thus removing the need of the Agent class to explicitly know the subtype of
newB and previousB and execute different conditional code based on the type
(which is almost always a no-no). This pattern is especially good in this
scenario if there are a large number of subtypes or you expect to add
numerous subtypes in the future. I wont go into the details of how to
impliment the factory pattern here - have a look at a design patterns book
for more info.

Thats how I would do it.

But maybee there is a better way? Anybody have a better way?

Zack.
 
M

Martin Magnusson

Ron said:
If I understand what you want, you need a concept sometimes
referred to as a virtual constructor. I call it a "clone" function.

Thanks, both of you! That's exactly it!

/ martin
 

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,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top