I think I should start by simplifying my class structure so I can better explain my problem, which I suspect might just be a misunderstanding of the use of virtual.
I have:
class Controller{
..
virtual void InitialiseController(){ //std::cout confirms this is running }
..
}
class AIController : public Controller{
..
virtual void InitialiseController(){ //some logic here }
..
}
class ComController : public AIController{
..
virtual void InitialiseController(){ //actually the same logic as parent }
..
}
My object, Snake, has a pointer to a Controller (Controller* _controller). When I call the snake.initialise(..) method I pass it a new ComController object which then sets snakes _controller equal to the new ComController. I know that that process works successfully.
But when I then call _controller.InitialiseController(); my debugger shows the program steps into the base class Controller’s blank implementation of InitialiseContoller.
I know I’ve probably oversimplified and you might not be able to help, but I think perhaps it’s something I’m not understanding about the whole concept, a logic error, rather than a typed error and would like to check.
Additional code:
_player2->Initialise(_gameProperties, &_fruitManager, new ComController(_player2), _player1);
stepping in ..
void Snake::Initialise(
GamePropertiesManager* gpm, FruitManager* fm, Controller* control, Snake* opposingSnake)
{
_game = gpm;
_fruitManager = fm;
_controller = control;
_opposition = opposingSnake;
if(_controller){
///Bunch of stuff in here runs just fine
// This primarily serves to ensure that a ComControllers timer is started, but other controllers might wish to override initialise later
_controller->IntialiseController();
}
}
I don’t really see anything wrong with what you’re doing (at least as far as understanding and using virtual methods).
Here’s a complete example. Please compare it with your code.
Compile:
g++ -Wall -pedantic -o tmp tmp.cppExecute:
ComController subclass controller...