0

Issue:

  • I have std::vector<callback_func_t> to store callback. callback_func_t is void(*)(void*)
  • void add_func(callback_func_t fn) is mathod to attach/add callbacks to vector using .push_back mathod.
  • Here is my demo code snippest. I want to attach callback of (Demo class) mathod to the add_func();
  • Wanted to attach Demo::testcb() using add_func()
  • Need help or solution for the same
#include <iostream>
#include <vector>
#include <list>
#include <functional>

using namespace std;
using namespace std::placeholders;

typedef void(*callback_func_t)(void*p);

vector <callback_func_t>funcs;

void testFunc1(void*p){
    
}

void testFunc2(void*p){
    
}

void add_func(callback_func_t fn){
    funcs.push_back(fn);
}

void print_func(){
    for(auto a:funcs) printf("cb: %p\r\n",a);
}

class Demo{
    public:
    Demo(){}
    ~Demo(){}
    
    void testcb(void*p){
        
    }
};

int main()
{
    Demo test;
    add_func(testFunc1);        // works okay
    add_func(testFunc2);        // works okay
    //add_func(bind(&Demo::testcb,&test,_1));        // wanted to attach test.testcb
    print_func();
    return 0;
}
Filburt
  • 17,626
  • 12
  • 64
  • 115
  • The result of `std::bind` is not `void(*)(void*)`. Store `std::function` in the vector. – 273K Oct 12 '22 at 06:02

0 Answers0