1

So I am trying to make a project that mirrors Ruby's Gosu gem, a 2d graphics plugin. To do this, I have created a class called Window, which is intended to be inherited from (class Game: public Window), and then have three of its functions defined: Update() Controls() and Draw().

To accomplish this in my window class I have a defined function Show() which looks like this

    void Window::Show(){
            // stuff to initialize and set up my window and controls
            glutTimerFunc(100,real_update,fps); 
    }

And my real_update function is simply

    void Window::real_update(int v){
            glutPostRedisplay();
            Update();
            glutTimerFunc(1000/fps,real_update,v);
    }

The logic here is that whatever class inherits Window, will be able to call Show(), which will set up the call back loop and initialize the window, then in the real_update loop it will call the Update which will be defined in the child class.

However, when I create a simple child class, define Update in the child class to just indicate it is being called, and then call Show on an instance of the child class it will only ever call the base class's (Window) definition for Update.

I'm certain this is a misunderstanding with inheritance, but I do not understand why. I thought that the child class call to Show() would call the base class's definition which then calls Update, which has been changed since the child class redefines it.

genpfault
  • 51,148
  • 11
  • 85
  • 139
user3086956
  • 55
  • 1
  • 7
  • 1
    What are you class declarations? – genpfault Jun 25 '14 at 19:15
  • My class Declarations are class Window and class Game: public Window. That is why I would expect Update to contain code written in Game and the function real_update to be inherited by Game and call Update when used int glutTimerFunct. – user3086956 Jun 25 '14 at 20:46

1 Answers1

0

The answer to this question was found in this thread Static Virtual functions in c++

Essentially, you want to have virtual like behavior but because I want to call glut functions it must be static, so you template your class, and have it call the template classes update function.

Community
  • 1
  • 1
user3086956
  • 55
  • 1
  • 7