Hi Martin,
"Martin Magnusson" <lo*******@frus tratedhousewive s.zzn.com>
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.