【发布时间】:2015-04-23 09:27:14
【问题描述】:
以下示例适用于传递不带参数的成员函数指针。有人可以解释我如何用参数来做到这一点吗?如果可能的话,我们也可以传递可变数量的参数吗?
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));
}
【问题讨论】:
-
你可以让
mF1取一个合适的std::function,并传入一个小 lambda,在调用时调用check2(2)。
标签: c++ function-pointers member-function-pointers