以下示例适用于传递不带任何参数的成员函数指针。有人能解释我如何用参数来做到这一点吗?如果有可能我们也可以传递可变数量的参数?如何将成员函数作为参数传递给另一个成员函数?
class test {
public:
typedef void (test::*check_fun_type)();
//typedef void (test::*check_fun_type)(int);
void mF1(check_fun_type ptr);
void check1();
void check2(int v1);
};
void test::check1() {
std::cout << "check1" << std::endl;
}
void test::check2(int v1) {
std::cout << "check2 " << v1 << std::endl;
}
void test::mF1(check_fun_type ptr) {
(this->*ptr)();
}
int main() {
test t1;
t1.check1();
t1.check2(2);
t1.mF1(&test::check1);
//t1.mF1((&test::check2)(2));
}
也参见http://en.cppreference.com/w/cpp/utility/functional/function HTTP来实现/en.cppreference.com/w/cpp/utility/functional/bind – senfen
你可以让'mF1'采取适当的['std :: function'](http://en.cppreference.com/w/cpp/utility/functional/function),并传入一个调用'check2(2)'的小lambda。 – BoBTFish