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 6785559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:08:30+00:00 2026-05-26T17:08:30+00:00

I have a class which I cant change (SuperClass) that has an interface for

  • 0

I have a class which I cant change (SuperClass) that has an interface for adding callback which I dont like very much, but I cant make it better. Is it possible to get rid of all the wrapper functions by using some template magic?

#include <iostream>
#include <string>
#include <vector>

enum EventType
{
    OkButtonPressed = 0,
    CancelButtonPressed,
    KeyPad1ButtonPressed,
    KeyPad2ButtonPressed,
    KeyPad3ButtonPressed,
    KeyPad4ButtonPressed,
    KeyPad5ButtonPressed,
    SomeOtherNonButtonEvent,
    EventType_count
};


// Not sure how the SuperClass is implemented, but from the outside all you can see is 
// the AddCallback(). The class can not be changed. Same applies to the callback typedef

typedef void (*EventCallback)(int argument, void *callbackOwner);
class SuperClass
{
private:
    #define CallbackPair std::pair<EventCallback, void*>
    std::vector<CallbackPair> m_callbackList[EventType_count];

public:
    void AddCallback(EventType eventType, EventCallback function, void * callbackOwner)
    {
        CallbackPair pair(function, callbackOwner);
        m_callbackList[eventType].push_back(pair);
    }

    void TriggerEvent(EventType type)
    {
        int someArguemt = (int)type;
        for(unsigned int i = 0; i < m_callbackList[type].size(); i++)
            m_callbackList[type][i].first(someArguemt, m_callbackList[type][i].second);
    }
};

class MyClass
{
public:
    void OnKeypadFunction(int argument){std::cout << "OnKeypadFunction called with " << argument << std::endl;}
    void OnOkFunction(int argument){std::cout << "OnOkFunction called with " << argument << std::endl;}
    void OnCancelFunction(int argument){std::cout << "OnCancelFunction called with " << argument << std::endl;}
};

// Wrapper functions which i would like to remove
static void s_OnKeypadFunctionWrapper(int arguemt, void *callbackOwner)
{
    ((MyClass*)callbackOwner)->OnKeypadFunction(arguemt);
}

static void s_OnOkFunctionWrapper(int arguemt, void *callbackOwner)
{
    ((MyClass*)callbackOwner)->OnOkFunction(arguemt);
}

static void s_OnCancelFunctionWrapper(int arguemt, void *callbackOwner)
{
    ((MyClass*)callbackOwner)->OnCancelFunction(arguemt);
}


int main()
{
    SuperClass super;
    MyClass myClass;

    // Register some callbacks
    super.AddCallback(KeyPad1ButtonPressed, s_OnKeypadFunctionWrapper, &myClass);
    super.AddCallback(KeyPad2ButtonPressed, s_OnKeypadFunctionWrapper, &myClass);
    super.AddCallback(KeyPad3ButtonPressed, s_OnKeypadFunctionWrapper, &myClass);
    super.AddCallback(KeyPad4ButtonPressed, s_OnKeypadFunctionWrapper, &myClass);
    super.AddCallback(KeyPad5ButtonPressed, s_OnKeypadFunctionWrapper, &myClass);
    super.AddCallback(OkButtonPressed, s_OnOkFunctionWrapper, &myClass);

    // How I would like to add the code..
    /*
    super.AddCallback(KeyPad1ButtonPressed, myClass->OnKeypadFunction, NULL);
    super.AddCallback(KeyPad2ButtonPressed, myClass->OnKeypadFunction, NULL);
    super.AddCallback(KeyPad3ButtonPressed, myClass->OnKeypadFunction, NULL);
    super.AddCallback(KeyPad4ButtonPressed, myClass->OnKeypadFunction, NULL);
    super.AddCallback(KeyPad5ButtonPressed, myClass->OnKeypadFunction, NULL);
    super.AddCallback(OkButtonPressed, myClass->s_OnOkFunctionWrapper, NULL);
    */


    // This is called some other place...
    super.TriggerEvent(OkButtonPressed);
    super.TriggerEvent(KeyPad1ButtonPressed);
    super.TriggerEvent(KeyPad5ButtonPressed);
}
  • 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-26T17:08:30+00:00Added an answer on May 26, 2026 at 5:08 pm

    How about something like this:

    template <typename T, void (T::*MemberFunction)(int)>
    struct CallbackHelper
    {
        static void EventHandler(int argument, void *callbackOwner)
        {
            auto myClass = static_cast<T*>(callbackOwner);
            ((myClass)->*(MemberFunction))(argument);
        }
    };
    

    Which would let you register your handlers like this:

    super.AddCallback(OkButtonPressed, CallbackHelper<MyClass, &MyClass::OnOkFunction>::EventHandler, &myClass);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have inherited a c# class 'Button' (which I can't change) which clashes with
I have a class which is marked with a custom attribute, like this: public
I have a class which has the following constructor public DelayCompositeDesigner(DelayComposite CompositeObject) { InitializeComponent();
I have a class which looks something like this: public class Test { private
I have a class which has many small functions. By small functions, I mean
I have some python module, which has a class ModuleClass and I can't modify
I have an interface Tree and an abstract class RBTree which implements this interface.
I have a static class 'Defaults' which shall hold default matrices that are forwarded
I have a Course class which contains collection of CourseItems . Each CourseItem has
I have a Java class that looks like this: public class My_ABC { int

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.