Im working on a little game-framework in c++. Currently i have a (base)class called "GameObject" from which all future in-game objects will derive from. Class enemy will be class Enemy : public GameObject and class Player will be class Player : public GameObject

This way, i can store all my gameobjects in an array of type GameObject and manage them easily and they will all have the basic game-object variables such as x/y/z coordinates, scale, etc. So what i eventually want is to be able to quickly create a new game-object by creating a new class and then simply inheriting from GameObject.

Know how in Unity every game-object monobehaviour script has a start function and an update method? That's what im trying to recreate, and my teacher has been able to recreate in the past using Java. (except he simply used the constructor instead of a "Start" method)

Here is where my problem comes in: I can't find a way to call the Update() method from all those classes that derive from GameObject. My teacher was able to get this working in a small java framework by putting this for-loop in the main-loop of the program:

if(_gameObjList.size() != null){
   for(int i = 0; i < _gameObjList.size(); i++)

Now im pretty sure that this is the correct c++ equilavent of that code:

if(_gameObjList.size() > 0){
   for(unsigned int i = 0; i < _gameObjList.size(); i++)

In the GameObject class of that java program there's an empty Update() method, which, if removed will break everything, and offcourse in every class that derives from GameObject there's also their own Update() method which for some reason gets called too, but does not give an error if removed. So this way all Update() methods from all classes that derive from GameObject get called even though from the code, it looks like it's just calling that Update() method from the GameObject class itself and not the classes that derive from it.

So now i wish to use this magic in my c++ program but it doesn't work. It just calls the Update method from the GameObject class and does nothing else :/ It won't call the Update functions of the classes that derive from it. So perhaps there are any sorcerers online on this forum who can explain this magic to me and how to use it? Or perhaps another way of executing all Update functions without calling all of them specifically?

Sidenote: if in the java program, i change the name of the Update() function in one of the classes that derive from GameObject, it won't be called/executed but doesn't give me any errors either. That specific object just won't be updated anymore.

Sorry for writing all this text, i just want to be as clear as possible.

  • \$\begingroup\$ You have to mark your GameObject::Update() method virtual (like so: virtual void Update() {}), and mark the methods that inherit from that as override (like so: void Update() override;). As a side note, in general, if you have an issue with the class, you should probably also include the code for that class (the GameObject and the classes that derive from it). \$\endgroup\$ – Vaillancourt Jul 27 '18 at 23:59

You'd want to use polymorphism. In your case :

class GameObject {
    //=0 means that child classes will have to override Update, otherwise 
    //they will be considered abstract
    //could be replaced with default logic
    virtual void Update() = 0;
    virtual ~GameObject() = default;

class Enemy : public GameObject {
    virtual void Update() override {
      //if GameObject implements default logic, add :
| improve this answer | |
  • 1
    \$\begingroup\$ Note: you don't need virtual if you write override, that's implied and redundant. \$\endgroup\$ – Vaillancourt Jul 28 '18 at 0:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.