E
E
Ensiouel2020-02-06 21:19:25
OOP
Ensiouel, 2020-02-06 21:19:25

How to make a "redefinition" of a function in c++?

Good day, I immediately ask you not to throw slippers.
Suppose I have some kind of Enemy class that has an update function , how can I make it so that I can "override" it, I know that the term is not particularly correct, I hope it will be clearer with an example code.

#include <iostream>
class Enemy {
    public:
    void update() {
    }
}
int main(int argc, char* argv[]) {
    Enemy enemy;
    /*И тут к примеру как нибудь так*/
    enemy::update() {
        std::cout << "Hello";
    }
    return 0;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Shatunov, 2020-02-06
@Ensiouel

Your question can be solved in this way.

#include <iostream>
#include <functional>

class Enemy final
{
public:
  std::function<void()> update = []() {};
};

int main( int argc, char* argv[] )
{
  Enemy enemy;
  enemy.update = []() { std::cout << "Hello"; };

  enemy.update();
  return 0;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question