Hello, I have a Player class that I want to be initialized with multiple derived character controller types, something that looks like so - but how do I find what controller type is being passed into the constructor so I can initialize a new one of that type?
The derived player classes have their own constructors, you only write the default, generic code in the base class' constructor/destructor and then handle all the other derivative-specific code in the derived class constructor and destructor.
Oh, so you want different control behaviors, I get it now. Yes, you can do that, as long as your derived classes override pre-set functions that implement the player behavior.
Player( CController * controller )
{
mController = controller;
// the player owns this controller now, don't delete it in the caller
};
// somewhere in the other part of the code galaxy ...
mPlayer = new Player( new JetpackController() );
Yeah, most of the time reinterpret_cast<> means: 1) bad design; 2) abandoned compile time type checking; 3) access violation at some later point. Don't use the run-time type information either - disable it in your compiler settings for the peace of mind and to save some memory along the way. Templates, interfaces, and virtual methods together give you everything you might need to vary behavior between various related types. They also make your code extensible almost for free if used right.