Say i have these classes
ViewA and ViewB
In objective C using the delegate pattern I could do
@protocol ViewBDelegate{
- (void) doSomething();
}
then in ViewB interface:
id<ViewBDelegate> delegate;
then in ViewA implementation i set the delegate:
viewB.delegate = self;
and now I can call in doSomething from viewB onto any that unknown type delegate.
[delegate doSomething];
“C++ How to Program” has been the worse read an can’t find simple examples that demonstrates basic design patterns.
What i’m looking for in C++ is:
- events ActionScript and java
- or delegates or NSNotifications in Objective C
anything that allows class A, Class B and Class C to know that ClassX
didSomething()!!!
thanks
If I were you, I wouldn’t use function pointers to accomplish this task. Leave this option to the gurus 😉
In Boost, there is a beautiful library called signals. It makes your life easier! This is an example of usage:
This will print:
Here is how it works.
First, you declare the place that holds the delegates:
Then, you “connect” it to what ever group of functions/functors/callable things you want to call.
Note, that I have used
bind. So, that the type of functions in the list_of_actions is the same, but we can connect it to different type of classes. So:This thing, produces a callable thing, of type
void ()as we declared the type oflist_of actionsearlier. Of course, you specify the instance you want to apply this member function on in the second parameter..If you are doing multi-threaded stuff, then use its sister signals2.
Hope that helps.