Re: Polymorphism without virtual in C++
On Aug 7, 12:02 pm, feel <feel...@gmail.com> wrote:
On Aug 7, 4:55 pm, James Kanze <james.ka...@gmail.com> wrote:
[...]
This sounds more like letter-envelope than like compilation
firewall. But I'm not sure I've fully understood it. (The
letter-envelope idiom is used to make a class with value
semantics behave polymorphically.)
Yes, it's just like that. But the difference is:we have to work on
many kind of Envelops.
In classic letter-envelop, we can hide the real data
deifinition. but if we have to work on a class heirarchy, for
example:
Entity
^
|
Curve
^
|
Line
In this case, the real data is in Line, but we have to catalog all the
line's method into Entity, curve.
Then we can work on all kind of entity or curve.In a summary, we have
two requirement:
1 data hide, different data implementation.
2 class heirarchy.
I'm not sure I understand. How is this different from the
classical letter-envelope? You declare all of the functions in
Entity, virtual, with an implementation which just forwards to
the letter class.
Or is it that the real "envelope" (which defines the interface)
is Curve, and Entity is just some sort of generic holder
(something like boost::any)? In that case, I'd implement Curve
as a true letter envelope, and provide for some sort of dynamic
casting to get a Curve from Entity, e.g.
class Entity
{
public:
template< typename Envelope >
operator Envelope() {
return Envelope(
dynamic_cast< Envelope& >( *myLetter ).clone() ) ;
}
private:
Entity* myLetter ;
} ;
(Note that this will throw std::bad_cast if Entity isn't really
the requested type.)
So if I have an Entity, and want to use it as a Curve (although
it is really a Line, of course):
Curve c = static_cast< Curve >( entity ) ;
(It's a bit wierd in that you need to use static_cast in order
to get a dynamic_cast. Perhaps a named function would be
preferable to the type conversion operator.)
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34