I'll agree with you there is more than one way to attack the problem. Mine was merely another suggestion, based on the idea that the "everything isAn Object" isn't always very useful. OOP, of course, isn't always the best solution... and it's just my opinion that the one base class paradigm unnecessarily complicates OOP design. IMHOP an "Object" is too general an abstraction to be useful, but that's why there's a million of us programmers out there and not just two
;)
C++ instanceof?
Personally, I'm behind the dynamic_cast<>() idea.
Its used to great effect in the HL2 source, specifically with the vehicles to cast one generic vehical type to a more specific one to check its interface.
The HL2 vehicle heirarchy uses multiple inhertiance so it has to swap between interfaces alot, if it had tried to push them all into the public interface of the class you'd have interface_bloat++;
Its used to great effect in the HL2 source, specifically with the vehicles to cast one generic vehical type to a more specific one to check its interface.
The HL2 vehicle heirarchy uses multiple inhertiance so it has to swap between interfaces alot, if it had tried to push them all into the public interface of the class you'd have interface_bloat++;
Personally I'd go with the QueryInterface method already suggested. Seems a lot more elegant to me, although I'd probably be anally retentive, and use templates to get rid of the void** in QueryInterface.
class Jeep : public Entity, public IVehicle{ //Implementations of IVehicle go here bool QueryInterface ( UInt interfaceID, void** interfacePointer ); };IVehicle* vehicle;if ( entity->QueryInterface(VEHICLE, vehicle) ){ vehicle->Board();}
how is that more elegant?
simple, compact, doesnt rely on you maintaining your code thus less potential mistakes and uses the language constructs... doesnt come much more elegant than that.
class Jeep : public Entity, public IVehicle{ //Implementations of Jeep go here};if ( IVehicle *entity = dynamic_cast<IVehicle*>(someentity)){ vehicle->Board();}
simple, compact, doesnt rely on you maintaining your code thus less potential mistakes and uses the language constructs... doesnt come much more elegant than that.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement