This is a "classic C++" post, something that's been part of the language since conception: How to use an operator in your class design.
I developed a callback class. Your "gizmo that needs to callback something" would hold an instance of that class (an object). And you 'd assign some function to that object.
Callback<int> cb; cb.set([]() -> void { start_video(); });
When the time is ripe (usually an event: an interrupt, a mouse click, UART data arrived, ...) you ask the callback class to execute that function. In this blog, I'm pretending that I'm a button, and someone clicked me. The effect should be that the start_video() callback gets executed.
In my original design, you reacted on that by calling the callback class call() method:
bool button1::clicked() { cb.call(); }
This would execute the start_video().
However, in C++ there is a standard way to indicate that something is a call and that you "execute the object" : the operator (). Similar to the operator +, that indicates that you want to add. I want that my class follows that paradigm.
This is how I want that a call should look like:
button1::clicked() { cb(); }
It again should call start_video(). Like the originall call() method did.
Implementing this isn't hard. In my Callback template class, I just had to replace call() by operator ():
class Callback { public: // ... inline void operator()() { // call the registered callback } // ... };
in reality, my class is template driven, and will accept callbacks with any type / count of parameters. I've removed that for blogging simplicity.
Opinions on this topic may vary. I think that operator() indicates perfectly that you want the callback class to execute the callback handler. Let me know if you have a different preference.