Deep copying a composite of virtual base classes.

Started by
3 comments, last by Succinct 22 years, 10 months ago
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~
-- Succinct(Don't listen to me)
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
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
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
-- Succinct(Don't listen to me)
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...
This is my signature. There are many like it, but this one is mine. My signature is my best friend. It is my life. I must master it as I must master my life. My signature, without me, is useless. Without my signature, I am useless.
Very neat, indeed!

This is a very informative thread!

Thanks, guys.
-- Succinct(Don't listen to me)

This topic is closed to new replies.

Advertisement