Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Copying an object using base class pointers

Reply
Thread Tools

Copying an object using base class pointers

 
 
Martin Magnusson
Guest
Posts: n/a
 
      09-24-2003
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

 
Reply With Quote
 
 
 
 
Ron Natalie
Guest
Posts: n/a
 
      09-24-2003

"Martin Magnusson" <> wrote in message news:bkt1mj$rlr$...
>. 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;
};


 
Reply With Quote
 
 
 
 
=?iso-8859-1?Q?Juli=E1n?= Albo
Guest
Posts: n/a
 
      09-24-2003
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.
 
Reply With Quote
 
paranoid?
Guest
Posts: n/a
 
      09-24-2003
Hi Martin,


"Martin Magnusson" <>
> 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.


 
Reply With Quote
 
Martin Magnusson
Guest
Posts: n/a
 
      09-24-2003
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

 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      09-24-2003

"Ron Natalie" <> wrote in message news:3f721022$0$51840$ m...
>


> Base* clone() { return new DerivedA(this); }


Gak...
new DerivedA(*this);


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
why should base class object points to derived class object (virtualfunction question) ? DanielJohnson C++ 7 01-17-2009 10:15 PM
slicing - copying or assigning derived object to base object subramanian100in@yahoo.com, India C++ 1 04-22-2008 02:55 PM
Object creation - Do we really need to create a parent for a derieved object - can't the base object just point to an already created base object jon wayne C++ 9 09-22-2005 02:06 AM
Access of base class' private base class: qualification required, why Alf P. Steinbach C++ 6 09-03-2005 04:03 PM
Passing derived class object array in place of base class object array justanotherguy63@yahoo.com C++ 9 12-03-2004 10:57 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57