C ++ function pointers and subclasses

I can choose between function pointers and subclass objects. To make it clear, say that I must notify some object of some action (for example, a timer); refer to the following two options (very simple code for demo purposes):

Version 1

typedef void TimerCallback(void *args); class Timer{ public: Timer(); ~Timer(); void schedule(TimerCallback *callback, void *args, long timeout)=0; void cancel(); }; 

Version 2

 class TimerTask{ public: TimerTask(); virtual ~TimerTask(); void timedout()=0; }; class Timer{ public: Timer(); virtual ~Timer(); void schedule(TimerTask *callback, long timeout)=0; void cancel(); }; 

which one is the standard C ++ way and which one is effective? Please let me know if you have any other suggestions in this regard.

Please let me know if I do not understand in this regard.

thanks

+4
source share
5 answers

I would say std::function and std::bind . Then it doesn’t matter if you want to use inherited classes, stand-alone functions, member functions or lambdas .


By the way, if anyone is interested, I did a simple processing of timer events some time ago, as an answer to another question. It demonstrates the use of, for example, std::function and std::bind : fooobar.com/questions/175424 / ....

+14
source

I think it's better to use boost(or std since C++11)::function to perform a callback and boost::bind to bind its arguments or to use boost::signal . This would be a more general and detailed solution for the price of a really small fine.

http://www.boost.org/doc/libs/1_53_0/doc/html/signals2.html

+4
source

You use object-oriented programming, and you must follow the paradigms of object-oriented programming.

In my opinion, using objects, not function pointers, is cleaner and usually the best way.

You can also try using a template to make the code even better and more flexible.

You can also consider the publisher / subscriber template .

+2
source

The function pointer effectively prevents the use of closures - assigning methods to the event handler (this is not entirely true, but it will limit you in such a way that this solution is not very useful).

I would vote for an object oriented approach. If you use C ++ 11, you can greatly simplify your code:

 #include <cstdio> #include <functional> class Emitter { private: std::function<void(int)> eventHandler; public: void SetEventHandler(std::function<void(int)> newEventHandler) { eventHandler = newEventHandler; } void EmitEvent() { eventHandler(42); // + error-checking } }; class Handler { private: void HandleEvent(int i) { printf("Event handled with i == %d\n", i); } public: void AttachEmitter(Emitter & e) { e.SetEventHandler([this](int i) { HandleEvent(i); }); } }; int main(int argc, char * argv[]) { Emitter e; Handler h; h.AttachEmitter(e); e.EmitEvent(); } 
+1
source

Both work. Your first is "C-style" and somewhere you need a static function. The second version is "C ++ style" and allows you to use an instance of TimerTask.

Version 2 should generally be used, as it eliminates the need for a static function.

0
source

All Articles