Pointers to a hierarchy of objects

Hi,

I have a hierarchy of classes like this:
* Base class: Object.
* Derived classes from Object: ObjectDerivedOne & ObjectDerivedTwo.
* Derived classes from ObjectDerivedTwo: ObjectUsable & ObjectUnusable.

Also, I'm working with a class like this one:
1
2
3
4
5
6
7
8
9
class Wrap{
  private:
   Object* o1;
   Object* o2;
  public:
   // Constructor, destructor and other methods
   Object* O1() const { return o1; }
   Object* O2() const { return o2; }
};

where o1 is always of type ObjectDerivedOne( at least for now ) and o2 can be of type ObjectDerivedTwo or its children.

What I want to do is something like this, in order to be able to work with local objects leaving the original objects untouched:
1
2
3
Wrap w( .... );
ObjectDerivedOne* od1 = new ObjectDerivedOne( *( w->O1() ) );
ObjectDerivedTwo* od2 = new ObjectDerivedTwo( *(w->O2() ) );

For the first object, I thought to define a constructor in ObjectDerivedOne that would instantiate the values of the attributes with the values of w->O1().
However, for the second one I'm not so sure this would work fine, since ObjectDerivedTwo has its own children.

I thought of using something like this:
1
2
3
4
5
6
7
8
switch( w->O2()->Type() ) {
  case ObjectUsable:
    od2 = new ObjectUsable(...);
  case ObjectUnusable:
    od2 = new ObjectUnusable(...);
  case ....:
  // more cases
}

but I think this would penalise performance if the hierarchy is too large.
Do you have any ideas? Maybe using a Factory pattern, for example?
Thanks in advance.
Sounds like you want the clone pattern:

http://www.cplusplus.com/forum/articles/18757/

basically you would do this:

1
2
3
4
5
6
7
// not this:
// ObjectDerivedOne* od1 = new ObjectDerivedOne( *( w->O1() ) );
// ObjectDerivedTwo* od2 = new ObjectDerivedTwo( *(w->O2() ) );

// instead you do this:
Object* o1 = w->O1()->clone();
Object* o2 = w->O2()->clone();


Also, note you have a bit of a contradiction here:

You have this in code:
1
2
   Object* o1;
   Object* o2;


But then you say this:
where o1 is always of type ObjectDerivedOne( at least for now ) and o2 can be of type ObjectDerivedTwo or its children.


If you really want to enforce a restriction that o1 is always ObjectDerivedOne, then it shouldn't be an Object* in that class... it should be an ObjectDerivedOne*.

Same with o2. If you want to enforce that, it should be an ObjectDerived2*.

You should do one of the below:
- don't assume o1 and o2 are anything other than Object*s
or
- change the definition of o1 and o2 to be a derived class
Last edited on
Thanks Disch.
I know about that contradiction, however I can't change it since I'm not the one who wrote the class, I'm just working with it.

I'll check that patter right away!
So the clone pattern is a simpler version of the prototype pattern, right? They both seem similar to me.

http://sourcemaking.com/design_patterns/prototype
It looks like the prototype pattern is an extension of the clone pattern. So basically yeah.

The clone pattern is to deep copy abstract objects. Creating new objects is done normally.


If I'm reading that page right, it looks like in the prototype pattern, you don't create new objects "by hand" like you would with the clone pattern. Instead you have one "prototype" object that is used to generate all future instances of the class. And you use the clone pattern to duplicate that object whenever you need a new object.

Although I'm not entirely sure what benefit the prototype pattern offers over just using new directly. So maybe I'm not understanding it correctly.
Topic archived. No new replies allowed.