How to make different implementation of the same function class in C++?

Suppose we have some model, and let's say we wanted to click worked any event. About this class will allow us to treat depression (implementation omitted):
Class Model{
Model();

public:
void click();
}


However, defining this function, we will not be able to make different models behave differently.
The crux of the matter is that for each instance of the Model class function click () was executed differently. And if you can, by overriding this function, change the number and types of arguments, it will do well. All in advance thanks for answers or suggestions to Google on the key words (I would be very grateful if they announce)
June 8th 19 at 17:03
3 answers
June 8th 19 at 17:05
Solution
To migrate custom functionality in 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;
}

Such students are in any visual window library: VCL, Qt. In VCL it is, with the exception inscribed in the property syntax. In Qt, this is done using signals slots.

To establish the transfer of any data in this feature — pattern "command".
class ClickEvent {
public:
 int x, y;
 virtual ~ClickEvent();
}

using EvClick = void (*)(ClickEvent&);
I don't quite understand this design, I properly understand that the query "student C" I can find detailed explanation? And if not difficult that you can read to find out what problems of a similar nature\level arise in the development? Can books on design patterns or algorithms? - Adrian_Sporer81 commented on June 8th 19 at 17:08
the design pattern called "delegation", but commonly known as "event listener". - Humberto_Lynch68 commented on June 8th 19 at 17:11
In a more complex version when one event listen as much as you want objects, then the "Observer". - Humberto_Lynch68 commented on June 8th 19 at 17:14
apparently, you need to start with simple things: readers, writers, subscribers and delegates...
GoF can be read on this topic.
On Gamedev Lectures are free paraphrase GoF for simplified taxation.
But strive, in terms of UI and not only, it is to this approach. - Benjamin_McClure commented on June 8th 19 at 17:17
,
what problems of this nature\level arise in the development?

For subscriptions main problem — to your mechanisms for subscribing to events was adequate for your needs. Qt allows a signal changed(int) to subscribe slot somethingChanged(). Allows you to send information from stream to stream. And much more. But need it be you? - Humberto_Lynch68 commented on June 8th 19 at 17:20
Of flow in the stream, of course not. So far:
Of course, you can use Qt (just him working now), but I think that sooner or later, you may need to work without it, as, for example, in the case described in the subject, and to such we must be prepared at the level of more difficult than to write functions and pass them pointers to each instance of the class. - Adrian_Sporer81 commented on June 8th 19 at 17:23
Thanks for the material! - Adrian_Sporer81 commented on June 8th 19 at 17:26
June 8th 19 at 17:07
Either store a pointer to a function en.cppreference.com/w/cpp/utility/functional/function (or raw), or make the base function virtual and it is possible both, for example.

class Base {
public:
 virtual ~Base() {}
 virtual doIt()
{
 if (callback)
callback();
}
private:
 std::function<void()> callback;</void()>

And inherited can be overridden doIt and it will do something else.
June 8th 19 at 17:09
The point is not that under each button sozdatel your class, and that was, in absolutely shameless, assign the function value to another function, in a sense now it's implemented, but I would like a little better
Model.h
void (*PointToMouseClick)() = NULL;
 void (*PointToMouseRelease)() = NULL;
 bool ClickAvailable;
...
 void MouseClick();
 void MouseRelease();


Model.cpp
void Model::MouseClick(){
if(PointToMouseClick!=NULL)
(*PointToMouseClick)();
}

void Model::MouseRelease(){
if(PointToMouseRelease!=NULL)
(*PointToMouseRelease)();
}


main.cpp
std::vector<model> Models;
 Models[32].PointToMouseClick=&ButtonOnPress;</model>


buttonevents.cpp
void ButtonOffPress(){
 for(int i = 0; i <15;i++)
 Models[i].Color = glm::vec3(0.0 f,0.0 f,0.0);
}
- Adrian_Sporer81 commented on June 8th 19 at 17:12

Find more questions by tags C++OOP