[C ++ std :: bind()成员函数的参数

问题描述 投票:0回答:2

我想使用成员函数的std::function并通过返回值提供它。它的语法std::bind(...)是什么?

class Test{
    int move(int x){
        return x * Y;
    }
    std::function<int(int)> getFunc(){
        std::function<int(int)> tmp2 
    std::bind(&Test::move, _1, this);
            return tmp2;
    }
};
c++ std function-pointers
2个回答
2
投票

应该为std::bind(&Test::move, this, _1);

Lambda是替代项:

std::function<int(int)> tmp2 = [this](int i) { return move(i); };

1
投票

首先,传递给this时应更改_1std::bind的位置。其次,Test::move应该返回int

class Test{
    int move(int x){
       return ...; 
    }

    std::function<int(int)> getFunc(){
        using namespace std::placeholders;
        std::function<int(int)> tmp2 = std::bind(&Test::move, this, _1);
        return tmp2;
    }
};
© www.soinside.com 2019 - 2024. All rights reserved.