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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T01:37:37+00:00 2026-06-11T01:37:37+00:00

I have a void function inside of a class. In old C++ i’d make

  • 0

I have a void function inside of a class. In old C++ i’d make a function static taking the class name as a parameter and had my own class which took a static void function + a void* for me to easily call it.

However that feels old school. It also isn’t templated which feels like i could be doing more. What is a more modern way of creating callbacks to myclassVar.voidReturnVoidParamFunc

  • 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-11T01:37:39+00:00Added an answer on June 11, 2026 at 1:37 am

    Use std::function and lambdas (or std::bind()) to store callables:

    #include <functional>
    #include <iostream>
    
    
    class Test
    {
    public:
          void blah() { std::cout << "BLAH!" << std::endl; }
    };
    
    class Bim
    {
    public:
          void operator()(){ std::cout << "BIM!" << std::endl; }
    };
    
    void boum() { std::cout << "BOUM!" << std::endl; }
    
    
    int main()
    {
        // store the member function of an object:
        Test test;  
        std::function< void() > callback = std::bind( &Test::blah, test );
        callback();
    
        // store a callable object (by copy)
        callback = Bim{};
        callback();
    
        // store the address of a static function
        callback = &boum;
        callback();
    
        // store a copy of a lambda (that is a callable object)
        callback = [&]{ test.blah(); }; // often clearer -and not more expensive- than std::bind()
        callback();
    }      
    

    Result:

    BLAH!

    BIM!

    BOUM!

    BLAH!

    Compiles and run: http://ideone.com/T6wVp

    std::function can be used as any copyiable object, so feel free to store it somewhere as a callback, like in object’s member. It also means that you can freely put it in standard containers, like std::vector< std::function< void () > > .

    Also note that equivalent boost::function and boost::bind have been available for years.

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

Sidebar

Related Questions

I have a function void foo() and inside foo I call the function void
I have: void add_all_msgs(std::deque<Message>::iterator &iter); How can I make that function generic, so it
Inside of a static member function I need to get the type. class MyClass
I've got a function inside a class (A) that essentially takes as a parameter
I'm trying to pass a void* to a function, then inside that function make
I have an asp web service that contains a class which has a function
I have this situation where I declare inside my main class a function that
I have this function: void func(int a, int b, char s, FILE *fp, int
I have this function: void ToUpper(char * S) { while (*S!=0) { *S=(*S >=
Suppose I have a function void myFun(int*) In C++ what exactly does the following

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.