0

What I have is the following:

class A{

public:
   virtual void OnStart() = 0;
   void GameLoop(){
      OnStart();
      //Do other stuff
   }

};

class B : public A{
   void OnStart() override{
      //Do Something
   }
};

Now I have the problem that it either doesn't call OnStart at all or it gives me an error. I have also tried this->OnStart() but none of the things I have tried worked.

Ams1901
  • 69
  • 1
  • 1
  • 6
  • 3
    Please show the code that has the error or works not how you expect. The code you posted does not have that issue, but others. For starters `virtual void OnStart = 0;` is not valid syntax – 463035818_is_not_an_ai Jun 21 '21 at 13:28
  • Does this answer your question? [Can I call a base class's virtual function if I'm overriding it?](https://stackoverflow.com/questions/672373/can-i-call-a-base-classs-virtual-function-if-im-overriding-it) – Lukas-T Jun 21 '21 at 13:30
  • I've tried your code [here](https://onecompiler.com/cpp/3x379s5eq). and it works with the added `()` brackets as suggested by the others. Just make sure you create an instance of class B and not of A. – Jan Gabriel Jun 21 '21 at 13:34

1 Answers1

2

You should declare the virtual OnStart as a function.

class A{

public:
   virtual void OnStart() = 0; // add () to declare it as a function (with no arguments)
   void GameLoop(){
      OnStart();
      //Do other stuff
   }

};

class B : public A{
   void OnStart() override{
      //Do Something
   }
};
MikeCAT
  • 73,922
  • 11
  • 45
  • 70