Jump to content

  • Log In with Google      Sign In   
  • Create Account

Deep copying a composite of virtual base classes.


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 Succinct   Members   -  Reputation: 169

Like
Likes
Like

Posted 02 June 2001 - 02:51 AM

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~

Sponsor:

#2 Shannon Barber   Moderators   -  Reputation: 1362

Like
Likes
Like

Posted 03 June 2001 - 04:11 PM

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


#3 Succinct   Members   -  Reputation: 169

Like
Likes
Like

Posted 04 June 2001 - 02:58 PM

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

#4 Mithrandir   Members   -  Reputation: 607

Like
Likes
Like

Posted 05 June 2001 - 04:02 AM

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...

#5 Succinct   Members   -  Reputation: 169

Like
Likes
Like

Posted 05 June 2001 - 02:19 PM

Very neat, indeed!

This is a very informative thread!

Thanks, guys.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS