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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T00:12:49+00:00 2026-06-08T00:12:49+00:00

I have a Class A which I intend to put in a shared library

  • 0

I have a Class A which I intend to put in a shared library as it interacts with the device drivers.

I have a Class B and may be C,D,E… in future which will use the class A using the shared library.

I want a capability of setting a callback function in Class A so that when a specific event occurs a non-static members function of class B,C,D,E … should be called by Class A.

I searched on google for callback function in C++ but found that non-static member functions are not supported by C-style definition of callbacks.

Can it be done using function pointers ?

Kindly give some suggestions for callbacks in C++ which do not violate the OOPS concepts.

I also came around a library called ‘Boost’ which offers similar functionality but I want to avoid the overhead of the extra library if possible. Is Boost recommended for callback function ?

EDIT : The B,C,D,E will not share any hierarchy and they will be completely independent classes. But all of them would have object of class A. And class A would also have a public function to set the callback function.

  • 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-06-08T00:12:50+00:00Added an answer on June 8, 2026 at 12:12 am

    One option, if you really really want to avoid the nearly unimportant overhead of a polymorphic function wrapper, is to make those functions static and have them take a “user data” void* parameter, pointing to an appropriate instance of the class the function is a member of. Inside the static function, you then cast back to the appropriate type:

    #include <iostream>
    
    struct A{
      typedef void (*callback_type)(void*, int);
      callback_type callback;
      void* user_data;
      void set_callback(callback_type cb, void* ud){
         callback = cb; user_data = ud;
      }
      void invoke(){ callback(user_data, 42); }
    };
    
    struct B{
      static void cb_foo(void* vself, int data){
        B* self = static_cast<B*>(vself);
        self->foo(data);
      }
      void foo(int data){ std::cout << data * 2 << "\n"; }
    };
    
    struct C{
      static void cb_bar(void* vself, int data){
        C* self = static_cast<C*>(vself);
        self->bar(data);
      }
      void bar(int data){ std::cout << data / 2 << "\n"; }
    };
    
    int main(){
      A a;
      B b;
      a.set_callback(&B::cb_foo, &b);
      a.invoke();
      C c;
      a.set_callback(&C::cb_bar, &c);
      a.invoke();
    }
    

    Live example on Ideone.

    I personally would recommend using std::function, though, since the above is severly limited in what can be accepted as a callback. std::function is a polymorphic function wrapper, meaning that it can take normal function pointers, member function pointers and even functors (function objects) and invoke them all in the same manner. Together with std::bind, which allows you to bind parameters to a function, you can make easy callbacks to member functions. Boost offers them too (Boost.Function, Boost.Bind).

    #include <iostream>
    #include <functional> // C++11
    //#include <boost/function.hpp>
    //#include <boost/bind.hpp>
    
    struct A{
      std::function<void(int)> callback;
      void invoke(){ callback(42); }
    };
    
    struct B{
      void foo(int data){ std::cout << data * 2 << "\n"; }
    };
    
    struct C{
      void bar(int data){ std::cout << data / 2 << "\n"; }
    };
    
    int main(){
      using namespace std::placeholders; // namespace for argument placeholders for std::bind
                                         // not needed for Boost.Bind
      A a;
      B b;
      a.callback = std::bind(&B::foo, &b, _1);
      a.invoke();
      C c;
      a.callback = std::bind(&C::bar, &c, _1);
      a.invoke();
    };
    

    Live example on Ideone.

    Basically std::bind does automatically what you had to do manually in the first version, it saves the object pointer and invokes the member function on it. It doesn’t do this through a void* pointer, however, and instead std::bind returns a different binder type for every different object pointer. That’s why you need std::function, since it doesn’t care what you pass it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application which we intend to makes use of an external library.
I'm writing a class which I intend to use to create subroutines, constructor as
I'm developing a library which the other programmer will import and use it for
I have a application using a sqlite DB which is put into an ArrayList.
I have a Class A which runs activity via startActivityForResult by passing Intent to
I have class which have one public method Start , one private method and
I have class LegacyClass which inherits OldBaseClass. I'm considering a change to introduce a
I have class Money which is an @Embeddable @Embeddable public class Money implements Serializable,
I have class A which extends the Activity class. This class is in package
I have class World which manages creation of object... After creation it calls afterCreation

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.