Re: The merits of dynamic_cast<>()
In article <decedae9-56ee-4ae7-937d-bf45c4699c24
@q35g2000vbi.googlegroups.com>, duggar@alum.mit.edu says...
class A {
public :
class ErrorIsNotB { } ;
class ErrorIsNotC { } ;
int fooA ( ) { return 0 ; }
int fooB ( ) { throw ErrorIsNotB() ; }
int fooC ( ) { throw ErrorIsNotC() ; }
} ;
class B : public A {
public :
int fooB ( ) { return 0 ; }
} ;
class C : public A {
public :
int fooC ( ) { return 0 ; }
} ;
I think a more intuitive set of objects needs to be hypothesized to show
the problem here:
class Vehicle
{
public:
virtual void turn(double rad) = 0;
virtual void forward(double speed) = 0;
// I put this here because I need to call
// it on cars that are Vehicle*
virtual void insert_gas(double amount) { throw "Wot?! 0_o."; }
};
class Car : Vehicle
{
public:
...implement vehicle interface...
void insert_gas(double amount) { ... }
};
class Bicycle : Vehicle
{
public:
...implement turn and forward...
};
Although *syntatically* we could say that the Bicycle appears to be a
Vehicle it actually isn't. Inserting gas into a bicycle is nonsense.
Thus we don't put "insert_gas" into Vehicle because it's convenient. We
take it out of that interface and put it where it belongs, in Car. Only
then will Bicycle be a proper subtype of Vehicle.