简体   繁体   English

虚函数绑定

[英]Virtual function binding

Hello guys have a question about virtual function binding do not know if there is such a thing so i want to ask about this . 大家好,有一个关于虚函数绑定的问题,不知道是否有这样的事情,所以我想问一下。 在此处输入图片说明

So i have a Game State machine. 所以我有一个游戏状态机。 GameState class is a main class for all state entities , GameStateFirstPhase is a class for entity only for those other 3 classes (StartUp,SelectServer,ConnectServer). GameState类是所有状态实体的主要类,GameStateFirstPhase仅是其他3个类(StartUp,SelectServer,ConnectServer)的实体类。

So what do i want to do is to split a draw function and draw all entity of those 3 classes inside a GameStateFirstPhase class, and those 3 classes have their own draw function for only specific things that are for that class, like GameStateSelectServer class would have to draw a UI Select server panel + entity of all classes which is GameStateFirstPhase class. 因此,我想做的是拆分一个draw函数并在GameStateFirstPhase类中绘制这3个类的所有实体,并且这3个类对于仅适用于该类的特定事物具有自己的draw函数,例如GameStateSelectServer类将具有绘制UI选择服务器面板+所有类的实体,即GameStateFirstPhase类。

This is how I want to bind CurrentState draw function with a GameStateFirstPhase Draw function: My game object have a pointer to a CurrentState and call it CurrentState Draw function . 这就是我想将CurrentState绘制函数与GameStateFirstPhase Draw函数绑定的方式:我的游戏对象有一个指向CurrentState的指针,并将其称为CurrentState Draw函数。

I have tried to call a Entity class draw function inside CurrentState Draw function like this: 我试图像这样在CurrentState Draw函数中调用Entity类的draw函数:

GameStateFirstPhase::Draw(gfx); 

but i think that's illegal to do . 但是我认为这样做是非法的。 I hope you get what i am trying to do and it makes sense . 我希望你能明白我的意思,这是有道理的。 I also give image which i explain everything . 我也给我解释一切的形象。

The question is if it possible to bind GameStateFirstPhase Draw function with a one of those states Draw functions (StartUp,SelectServer,ConnectSeerver state) bind i mean to get that parent behavior and add a child draw as well 问题是,是否有可能将GameStateFirstPhase Draw函数与这些状态之一绑定Draw函数(StartUp,SelectServer,ConnectSeerver状态)bind我的意思是要获得该父行为并添加子绘制

"...bind i mean to get that parent behavior and add a child draw as well." “ ...绑定,我的意思是得到父母的行为,并添加孩子绘画。”

If you get around to asking about polymorphism, 'bind' is not the useable term. 如果您四处询问多态性,则“绑定”不是可用的术语。 Research 'virtual', this is a modifier about a method. 研究“虚拟”,这是一种方法的修饰符。

No, the base class (parent) behavior (call it 'foo()') is not involved when a derived foo() is invoked. 不,调用派生的foo()时不会涉及基类(父)行为(称为“ foo()”)。 The derived foo() is not 'added' to the parent foo() in the sense of a sequence of method invocations. 从方法调用序列的意义上讲,派生的foo()不会“添加”到父foo()中。

However, the derived foo() can invoke the base foo(), at any point in its execution. 但是,派生的foo()可以在其执行的任何时候调用基本foo()。


update - add small example base / derived / use of virtual 更新-添加小示例库/派生/使用虚拟

#include <iostream>
#include <vector>


 // base class
 class Foo
 {
 public:
    Foo() { std::cout << "Foo::Foo() " << std::endl; }

    virtual
    ~Foo() { std::cout << "Foo::~Foo() " << std::endl; }

    virtual void foo();
 };

 class Bar : public Foo
 {
 public:
    Bar() : Foo()
       { std::cout << "Bar::Bar() " << std::endl; }

    virtual
    ~Bar() { std::cout << "Bar::~Bar() " << std::endl; }

    virtual void foo();
 };

 void Foo::foo()  // virtual method
 {
    std::cout << " -- foo() in Foo " << std::endl;
 }

 void Bar::foo()  // also virtual method
 {
    Foo::foo();
    std::cout << " -- foo() in Bar " << std::endl;
    Foo::foo();
    std::cout << " -- foo() in Bar " << std::endl;
    Foo::foo();
 }


int testFoo(void)  // invoke from main
{
   std::cout << "\nsizeof(Foo): " << sizeof(Foo) << std::endl;
   std::cout << "sizeof(Foo*): " << sizeof(Foo*) << std::endl;

   std::cout << "\n";
   Foo* f = new Foo();   // ctor Foo one time

   std::cout << "\n";
   Bar* b = new Bar();   // ctor Foo + ctor Bar

   std::cout << "\n-----------------\n";
   f->foo();             // foo() in Foo

   std::cout << "\n";
   b->foo();             // foo() in xxx (5 lines)
   std::cout << "\n-----------------";

   // create a polymorphic vector
   std::vector<Foo*> fooVec;

   fooVec.push_back(f);  // Foo* added
   fooVec.push_back(b);  // Bar* added - accepted because "is-a" Foo
   // maybe 100's of different derived methods, added in arbitrary order

   for (auto x : fooVec )  // invoke foo() on ALL elements
   {
      std::cout << "\n";
      x->foo();
   }

   return(0);
}

output 输出

sizeof(Foo): 8
sizeof(Foo*): 8

Foo::Foo() 

Foo::Foo() 
Bar::Bar() 

----------------- 
 -- foo() in Foo 

 -- foo() in Foo 
 -- foo() in Bar 
 -- foo() in Foo 
 -- foo() in Bar 
 -- foo() in Foo 

-----------------
 -- foo() in Foo 

 -- foo() in Foo 
 -- foo() in Bar 
 -- foo() in Foo 
 -- foo() in Bar 
 -- foo() in Foo 

Sorry this is so trivial ... my hope is that it helps lead you into research of virtual, pure virtual, etc. 抱歉,这太琐碎了……我希望它能帮助您进入虚拟,纯虚拟等方面的研究。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM