Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4031810
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:38:36+00:00 2026-05-20T11:38:36+00:00

I wanted to implement a C# event in C++ just to see if I

  • 0

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 can’t? 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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T11:38:36+00:00Added an answer on May 20, 2026 at 11:38 am

    If you can use Boost, consider using Boost.Signals2, which provides signals-slots/events/observers functionality. It’s straightforward and easy to use and is quite flexible. Boost.Signals2 also allows you to register arbitrary callable objects (like functors or bound member functions), so it’s more flexible, and it has a lot of functionality to help you manage object lifetimes correctly.


    If you are trying to implement it yourself, you are on the right track. You have a problem, though: what, exactly, do you want to do with the values returned from each of the registered functions? You can only return one value from operator(), so you have to decide whether you want to return nothing, or one of the results, or somehow aggregate the results.

    Assuming we want to ignore the results, it’s quite straightforward to implement this, but it’s a bit easier if you take each of the parameter types as a separate template type parameter (alternatively, you could use something like Boost.TypeTraits, which allows you to easily dissect a function type):

    template <typename TArg0>
    class MyEvent
    {
        typedef void(*FuncPtr)(TArg0);
        typedef std::deque<FuncPtr> FuncPtrSeq;
    
        FuncPtrSeq ls;
    public:
        MyEvent& operator +=(FuncPtr f)
        {
            ls.push_back(f);
            return *this;
        }
    
        void operator()(TArg0 x) 
        { 
            for (typename FuncPtrSeq::iterator it(ls.begin()); it != ls.end(); ++it)
                (*it)(x);
        }
    };
    

    This requires the registered function to have a void return type. To be able to accept functions with any return type, you can change FuncPtr to be

    typedef std::function<void(TArg0)> FuncPtr;
    

    (or use boost::function or std::tr1::function if you don’t have the C++0x version available). If you do want to do something with the return values, you can take the return type as another template parameter to MyEvent. That should be relatively straightforward to do.

    With the above implementation, the following should work:

    void test(float) { }
    
    int main() 
    {
        MyEvent<float> e;
        e += test;
        e(42);
    }
    

    Another approach, which allows you to support different arities of events, would be to use a single type parameter for the function type and have several overloaded operator() overloads, each taking a different number of arguments. These overloads have to be templates, otherwise you’ll get compilation errors for any overload not matching the actual arity of the event. Here’s a workable example:

    template <typename TFunc>
    class MyEvent
    {
        typedef typename std::add_pointer<TFunc>::type FuncPtr;
        typedef std::deque<FuncPtr> FuncPtrSeq;
    
        FuncPtrSeq ls;
    public:
        MyEvent& operator +=(FuncPtr f)
        {
            ls.push_back(f);
            return *this;
        }
    
        template <typename TArg0>
        void operator()(TArg0 a1) 
        { 
            for (typename FuncPtrSeq::iterator it(ls.begin()); it != ls.end(); ++it)
                (*it)(a1);
        }
    
        template <typename TArg0, typename TArg1>
        void operator()(const TArg0& a1, const TArg1& a2)
        {
            for (typename FuncPtrSeq::iterator it(ls.begin()); it != ls.end(); ++it)
                (*it)(a1, a2);
        }
    };  
    

    (I’ve used std::add_pointer from C++0x here, but this type modifier can also be found in Boost and C++ TR1; it just makes it a little cleaner to use the function template since you can use a function type directly; you don’t have to use a function pointer type.) Here’s a usage example:

    void test1(float) { }
    void test2(float, float) { }
    
    int main()
    {
        MyEvent<void(float)> e1;
    
        e1 += test1;
        e1(42);
    
        MyEvent<void(float, float)> e2;
        e2 += test2;
        e2(42, 42);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just wanted to see if I could have your thoughts on the design
i just loved the fullcalendar and wanted to implement it in a small application,
I'm porting some code from lisp, but I got stuck at this part (apparently
so I wanted to implement tab views on my app. But I realised the
I wanted to implement python com server using win32com extensions. Then consume the server
I wanted to implement the game Pacman. For the AI, I was thinking of
I wanted to implement a simple python program using parallel execution. It's I/O bound,
If I wanted to implement an app for 2 Android devices so I can
Let's say you wanted to implement a breadth-first search of a binary tree recursively
I wanted to understand how we can implement a safe logout method in a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.