Answer the question
In order to leave comments, you need to log in
How to make different implementation of the same class function in C++?
Suppose we have a certain model, and let's say we wanted an event to fire on click. Approximately such a class will allow us to process a click (we will omit the implementation):
Class Model{
Model();
public:
void click();
}
Answer the question
In order to leave comments, you need to log in
Transfer user functionality to another place - the so-called "listener".
using EvClick = void (*)();
Class Model{
public:
void click() { if (fOnClick) fOnClick(); }
void setOnClick(EvClick x) { fOnClick = x; }
private:
EvClick fOnClick = nullptr;
}
class ClickEvent {
public:
int x, y;
virtual ~ClickEvent();
}
using EvClick = void (*)(ClickEvent&);
Either store a function pointer en.cppreference.com/w/cpp/utility/functional/function (well, or raw), or make the base function virtual, or both, like this.
class Base {
public:
virtual ~Base() {}
virtual doIt()
{
if (callback)
callback();
}
private:
std::function<void()> callback;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question