多态性误解/重新定义虚拟函数不工作 [英] Polymorphism misunderstanding / redefined virtual function not working

查看:163
本文介绍了多态性误解/重新定义虚拟函数不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我应该从简化我的类结构开始,所以我可以更好地解释我的问题,我怀疑可能只是对使用虚拟的误解。

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.

我有:

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 }
..
}

我的对象Snake有一个指向控制器(Controller * _controller)的指针。当我调用 snake.initialise(..)方法时,我传递一个新的ComController对象,然后设置snakes _controller等于新的ComController。

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.

但是当我调用 _controller.InitialiseController(); 程序进入基类Controller的空白实现的InitialiseContoller。

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

正在加载...

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.

#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;
}

编译:
g ++ -Wall - pedantic -o tmp tmp.cpp

执行:
ComController子类控制器...

这篇关于多态性误解/重新定义虚拟函数不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆