Polymorphism misunderstanding / redefined virtual function not working

∥☆過路亽.° 提交于 2020-01-16 15:43:51

问题


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();

   }

}

回答1:


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.

#include <stdio.h>

class Controller{
public:
    virtual void InitialiseController(){
      printf ("base class controller...\n");
    }
};

class AIController : public Controller{
public:
    virtual void InitialiseController(){
      printf ("AIController subclass controller...\n");
    }
};

class ComController : public AIController{
public:
    virtual void InitialiseController(){
      printf ("ComController subclass controller...\n");
    }
};

int main (int argc, char *argv[])
{
   Controller *myController = new ComController ();
   myController->InitialiseController ();
   return 0;
}

Compile: g++ -Wall -pedantic -o tmp tmp.cpp

Execute: ComController subclass controller...




回答2:


I think you're calling the virtual method on the object directly. Polymorphism kicks in only if you call via a pointer or reference.

Edit: You write both _controller.InitialiseController(); and _controller->InitialiseController(); (was that there before?), so I'm not entirely sure what you're doing.



来源:https://stackoverflow.com/questions/10339612/polymorphism-misunderstanding-redefined-virtual-function-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!