• Advertisement

Archived

This topic is now archived and is closed to further replies.

Deep copying a composite of virtual base classes.

This topic is 6076 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

How would I perform a deep copy of an object composed of base classes? I have a class, "glMesh", that maintains a linked list of "glSurface"s. glSurface is a virtual base class that concrete surface classes derive from, and any actual glMesh will contain these derived classes, not just glSurfaces. Is there any way to perform a deep copy of a glMesh w/o defering the copying to a subclass? I could make glMesh a virtual base class, too, and hide the copy constructor/assignment operator so derived classes are responsible for copying their own list of glSurfaces, but the copying is too similar to do that. Any suggestions, guys? Thank you for your bandwidth. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~Succinct Demos Online~

Share this post


Link to post
Share on other sites
Advertisement
The methods I;ve read about involve adding something like
virutal BOOL CBaseObject::Clone(CBaseObject** hClone)=0
to the abstract base object, then each sub-class is responsible for creating a copy of itself and returning a base class pointer to it.

That what you were after or trying to avoid?

And did you mean that glSurface was an abstract base class?



Magmai Kai Holmlor
- The disgruntled & disillusioned

Share this post


Link to post
Share on other sites
Yes, I meant abstract base class.

What I was trying to avoid was requiring each subclass''s copy constructor to copy all of the members up the chain.

Thank you, Magmai Kai Holmlor.
As always, your answer is simple and elegant.

-- Succinct

Share this post


Link to post
Share on other sites
recursion!

it is the simplest solution to the problem at hand:

as magmai said, use a virtual constructor (factory method) that clones the object.

every object that composes another object calls the composed object''s clone method as well.

  
class parent
{
public:
parent* clone() = 0;
};

class A : public parent
{
public:
parent* clone()
{
A* temp = new A();
temp->myB = myB->clone();
temp->myC = myC->clone();
return temp;
}
private:
B* myB;
C* myC;
};

class B : public parent
{
public:
parent* clone()
{
B* temp = new B();
temp->myC = myC->clone();
return temp;
}
private:
C* myC;
};

class C : public parent
{
public:
parent* clone()
{
C* temp = new C();
// whatever...

return temp;
}
};


  
A
/ \
B C
|
C


so, A has no idea what B has, and B can be changed to contain different things, and the only thing that needs to be changed is B''s clone() method, and A''s clone method will work without any changes.

Neat, huh?

===============================================
It''s either ether or the other,
I press my window to the glass,
the glass turns to gas...
I''m going upstairs now, to turn my mind off...
to turn my mind off...

Share this post


Link to post
Share on other sites
Very neat, indeed!

This is a very informative thread!

Thanks, guys.

Share this post


Link to post
Share on other sites

  • Advertisement