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.