Need Advice ... Can I have a class member look like another type to not loose extra memory space
Posted on 2001-07-24
This probably is a stupid question but here goes ...
Derived11 Derived12 Derived13
Derived1 has a member :
Derived1 *DerivedParent ;
And Derived11, 12 and 13 make use of this DerivedParent to get certain data.
I now come to the conclusion that maybe I should have placed a Parent in Base :
because other classes derived from Base could use the member too + it would make creating a special other class easier too.
To not have to rewrite all the classes that now DerivedParent as a Derived class instead of Base class I was wondering if I could put the member in Base but have it look like all's the same ???
I can have a Parent in Base AND Derived1
and in the Derived constructor write :
Base::Parent = Parent ;
But then I would have two pointers instead of one (I can miss the extra 4 bytes per object)
So I was wondering if it is not possble to have the Parent in Base but some sort of ???don't know??? in Derived1 that doesn't take physical space and looks like a pointer to Derived1. ???
I could also In Derived1 change DerivedParent to a function DerivedParent() (still re-write required but minimal).
But then I would have to dynamic_cast the Base pointer :
return dynamic_cast<DerivedParent*>(Base::Parent) ;
But then I ask myself dynamic_cast is this slow ??
What does it take for code to constantly check with dynamic_cast ??
Hope this all makes sense.
I'm new to OOP but I'm learning !!!