In C ++, there are any events / delegates / interfaces / notifications! something?

Say I have these ViewA and ViewB classes

In a C object using a delegate pattern, I could do

@protocol ViewBDelegate{ - (void) doSomething(); } 

then in the ViewB interface:

 id<ViewBDelegate> delegate; 

then in the ViewA implementation I set the delegate:

 viewB.delegate = self; 

and now I can call doSomething from viewB on any delegate of unknown type.

 [delegate doSomething]; 

"C ++ How to Program" was worse, read, you can not find simple examples that demonstrate the basic design patterns.

What I'm looking for in C ++:

  • ActionScript and java events
  • or delegates or NSNotifications in Objective-C

everything that allows classes A, class B and class C to know that ClassX didSomething () !!!

thanks

+7
c ++ callback signals-slots
source share
7 answers

If I were you, I would not use function pointers to accomplish this task. Leave this opportunity to the guru;)

Boost has a beautiful library called signals . It makes your life easier! This is a usage example:

 #include <iostream> #include <boost/bind.hpp> #include <boost/signal.hpp> using namespace std; using namespace boost; struct A { void A_action() { cout << "A::A_action();" << endl; } }; struct B { void B_action() { cout << "B::B_action();" << endl; } }; struct C { void C_action() { cout << "C::C_action();" << endl; } }; struct X { // Put all the functions you want to notify! signal<void()> list_of_actions; void do_something() { std::cout << "Hello I am X!" << endl; list_of_actions(); // send notifications to all functions in the list! } }; int main() { X x; A a; B b; C c; x.list_of_actions.connect(bind(&A::A_action, a)); x.list_of_actions.connect(bind(&B::B_action, b)); x.list_of_actions.connect(bind(&C::C_action, c)); x.do_something(); } 

This will print:

 Hello I am X! A::A_action(); B::B_action(); C::C_action(); 

Here's how it works.

First you declare the place where delegates are stored:

 signal<void()> list_of_actions; 

Then you β€œconnect” it to any group of functions / functions / called things that you want to call.

 x.list_of_actions.connect(bind(&A::A_action, a)); x.list_of_actions.connect(bind(&B::B_action, b)); x.list_of_actions.connect(bind(&C::C_action, c)); 

Please note that I used bind . So, the type of functions in list_of_actions is the same, but we can connect it to different types of classes. So:

 bind(&A::A_action, a) 

This thing creates a calling thing like void () , as we previously declared a list_of actions type. Of course, you specify the instance that you want to apply this member function in the second parameter.

If you are doing multithreaded things, use his sister signals2 .

Hope this helps.

+9
source share

everything that allows class A, class B and Class C to know that ClassX didSomething () !!!

Perhaps you are looking for signals and slots that have several implementations:

I am sure that there are more of them, but these are the most important of which I know.

+5
source share

No delegates / events / etc.

You can simulate interfaces using a pure virtual function, see here for a similar question.

And there are function pointers ...

So basically the answer to your question is no, in C ++ there is not one of them (I don’t know about the latest standard), but there are replacements and workarounds.

+2
source share

Personally, I like Directly Fast C ++ delegates from Sergei Ryazanov. Very neat and easy to use implementation, declared faster than Boost::function .

You can also find events here. However, I do not use them. I implemented my own event handling using .NET style (sender, args).

+2
source share

Neither the C ++ language nor the associated standard library has delegates or events. Of course, there are many libraries that implement such things, or you can implement them yourself.

+1
source share

All I know is a method called call-back (in fact, a function pointer ).

Delegation? This is just a kind of wrapped callback method, and looks promising.

+1
source share

"C ++ How to program" was worse, read, you can not find simple examples that demonstrate the basic design patterns

I think the original book of design templates contains examples of how to implement each template using C ++.

+1
source share

All Articles