Is it possible to implement events in C++?

Posted by acidzombie24 on Stack Overflow See other posts from Stack Overflow or by acidzombie24
Published on 2011-03-05T14:59:57Z Indexed on 2011/03/05 15:25 UTC
Read the original article Hit count: 165

I wanted to implement a C# event in C++ just to see if i could do it. I got stuck, i know the bottom is wrong but what i realize my biggest problem is...

How do i overload the () operator to be whatever is in T in this case int func(float)? I cant? can i? Can i implement a good alternative?

#include <deque>
using namespace std;

typedef int(*MyFunc)(float);

template<class T>
class MyEvent
{
    deque<T> ls;
public:
    MyEvent& operator +=(T t)
    {
        ls.push_back(t);
        return *this;
    }
};
static int test(float f){return (int)f; }
int main(){
    MyEvent<MyFunc> e;
    e += test;
}

© Stack Overflow or respective owner

Related posts about c++

Related posts about class