9

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

Bach
  • 2,684
  • 5
  • 26
  • 36

7 Answers7

9

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:

#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 is how it works.

First, you declare the place that holds the delegates:

signal<void()> list_of_actions;

Then, you "connect" it to what ever group of functions/functors/callable things 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));

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:

bind(&A::A_action, a)

This thing, produces a callable thing, of type void () as we declared the type of list_of actions earlier. 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.

Khaled Alshaya
  • 94,250
  • 39
  • 176
  • 234
  • thanks Arak for taking the time and adding the example. that concludes my search. Brilliant! – Bach Jun 13 '10 at 10:22
5

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

Probably you are looking for signals & slots, which has multiple implementations:

I'm sure there are more, but these are the most significant of which I'm aware.

sml
  • 2,160
  • 13
  • 12
2

There are no delegates/events/etc.

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

And there are the function pointers...

So basically the answer to you question is no, there are none of that in C++ (don't know about the latest standard), but there are substitutes and workarounds..

Community
  • 1
  • 1
Unknown
  • 5,722
  • 5
  • 43
  • 64
2

Personally I like The Impossibly Fast C++ Delegates by Sergey Ryazanov. Very neat and easy to use implementation, claimed to be faster than Boost::function.

You can find events implemented there, too. I don't use them, however. I implemented my own event handling using .NET style (sender, args).

Aoi Karasu
  • 3,730
  • 3
  • 37
  • 61
  • Also worth looking at is [Lightweight Generic C++ Callbacks (or, Yet Another Delegate Implementation)](http://www.codeproject.com/Articles/136799/Lightweight-Generic-C-Callbacks-or-Yet-Another-Del). – Petr Vepřek Jun 08 '15 at 06:53
1

Neither the C++ language, nor its associated Standard Library, either have delegates or events. There are of course many libraries that implement such things, or you can implement them yourself.

  • thanks Neil. I'm really finding it difficult learning the language, I I realized how spoiling can Objective C and python be! – Bach Jun 13 '10 at 09:12
  • 2
    @Bach When learning any language, it's important not to try to carry over memes from other languages you know. C++ is really nothing like Python - I can't speak about Objective C. And note that you CAN do delegation in C++ - it's just not part of the language. –  Jun 13 '10 at 09:16
  • I agree Neil although i can't help it, as I know exactly what I want to do, and I look at how it's done in other languages I've worked with, so I naturally try to find the equivalent in C++. All part of discovering and learning I suppose. Will check out all the suggestions here. Stackoverflow has beaten any book I've read about any language, informative and full of professionals. thanks everyone. – Bach Jun 13 '10 at 10:16
1

All i know there is a type of method called call-back method(In fact, function pointer).

Delegation? It's just a kind of wrapped call-back method, and, looks advanced

rhapsodyn
  • 3,272
  • 5
  • 29
  • 36
  • thanks rhapsodyn. I guess i took advanced for granted. nothing's elegant about C++ coming from objective C. anyway thanks for the help. – Bach Jun 13 '10 at 09:14
1

"C++ How to Program" has been the worse read an can't find simple examples that demonstrates basic design patterns

I think that the original Design Patterns book includes examples of how to implement each pattern using C++.

ChrisW
  • 54,973
  • 13
  • 116
  • 224