std::function with non-static member functions

后端 未结 3 726
我寻月下人不归
我寻月下人不归 2021-01-01 22:54

i\'m trying to understand a concept and an error. what\'s wrong with this?

class A
{
public:
    A()
    {
        std::function testFunc(&a         


        
3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-01 23:15

    my question, is it possible to create any sort of object that is able to call a member of a specific instance

    In this case the only information that is missing is in fact what specific instance the std::function object should use: &A::func can't be used on its own (for instance (this->*&A::func)(0) uses &A::func with instance *this). Try:

    std::function testFunc = std::bind(&A::func, this);
    

    (Be careful that std::bind(&A::func, *this) and std::bind(&A::func, this) have slightly different semantics.)

提交回复
热议问题