By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,261 Members | 2,662 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,261 IT Pros & Developers. It's quick & easy.

Copying an object using base class pointers

P: n/a
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

Jul 19 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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.
Jul 19 '05 #2

P: n/a

"Martin Magnusson" <lo*******@frustratedhousewives.zzn.com> wrote in message news:bk**********@green.tninet.se...
. 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;
};
Jul 19 '05 #3

P: n/a
Hi Martin,
"Martin Magnusson" <lo*******@frustratedhousewives.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.
Jul 19 '05 #4

P: n/a
Ron Natalie wrote:
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

Jul 19 '05 #5

P: n/a

"Ron Natalie" <ro*@sensor.com> wrote in message news:3f***********************@news.newshosting.co m...
Base* clone() { return new DerivedA(this); }


Gak...
new DerivedA(*this);
Jul 19 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.