简体   繁体   中英

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

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.

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 .

I have tried to call a Entity class draw function inside CurrentState Draw function like this:

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

"...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. The derived foo() is not 'added' to the parent foo() in the sense of a sequence of method invocations.

However, the derived foo() can invoke the base foo(), at any point in its execution.


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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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