jeudi 2 janvier 2020

assigen member function ptr as a call back from another function

I have class A that has function ptr for a callback, the default values are some function ptr from class A but I wont to assign function ptr from class B I tried std::function and std::bind but it's not working I tried to use this answer but without success

the code is this:

#include <iostream>
#include <string>
#include <functional>

class A{
    public:
    typedef void(*action)() ;

    A( std::function<void()> a  = nullptr)
    {
        if(a)
            m_Func = a; 
        else
            m_Func = std::bind( (this->*(this->dummyFunction))() );
    }
    std::function<void(void)> m_Func;
    void dummyFunction(void){std::cout<<"dummy function\n" <<std::endl;}

};

class B{
    public:

    std::function<void()> m_Bfunc(){ std::cout << "class B func\n" <<std::endl;}

};

int main()
{
  B b;
  A a(b.m_Bfunc);
  a.m_Func();
}

and I wont that the function m_Bfunc will run

i tried using

Aucun commentaire:

Enregistrer un commentaire