函数指向类成员函数的指针 [英] Function pointer to class member function
问题描述
我想让函数指针作为参数。
I want to make function which has function pointer as a parameter.
#include <iostream>
using namespace std;
class test{
public:
test(){};
double tt(double input){
return input;
};
};
double fptr_test(double (*fptr)(double), double input){
return fptr(input);
}
int main(){
test t;
cout << t.tt(3) << endl;
cout << fptr_test(t.tt, 3) << endl; // This line doesn't work
cout << fptr_test(&test::tt, 3) << endl; // This line can't compile
return 1;
}
但它不工作。
我如何传递类成员函数作为参数?
But it doesn't work. How could I pass class member function as a parameter?
我可以在没有实例化的情况下调用成员函数吗?
Can I call the member function without instantiation?
推荐答案
函数指针和成员函数指针具有不兼容的类型。例如,& test :: tt
的类型是
A function pointer and a member function pointer have incompatible types. For example, the type of &test::tt
is
double (test::*)(double)
而不是
double (*)(double)
这个区别的原因是一个[非 - static
]成员函数有一个隐藏参数:成员函数应用的对象的指针, code> this 。从成员函数中移除正常函数指针的方法是通过提供这个
指针的函数进行委托,因此需要一个额外的参数。
The reason for this difference is that a [non-static
] member function has a hidden parameter: the pointer to the object the member function is applied, too, i.e., this
. The way to a normal function pointer out of a member function is to delegate via a function which supplies the this
pointer and, thus, takes an extra argument.
在C ++中,不是将函数指针作为函数的参数,这些函数可以由函数定制,而是采用一个函数对象。这种方法有两种风格:
In C++ it is much more useful to not take function pointers as arguments to functions which can be customized by a function but rather to take a function object. This approach comes in two flavors:
-
快速的方法是让函数对象类型为模板参数,函数对象。例如,
fptr_test()
将如下所示:
template <typename Fun>
double fptr_test(Fun fun, double input) {
return fun(input);
}
使用的隐含 a double
参数,其结果可转换为 double
。
The implicit concept used is a function callable with a double
argument which yields are result convertible to double
.
特别是当被调用的函数需要单独编译时,使用模板为每种类型的函数对象是不可行的。在这种情况下,使用类型擦除的表示,即 std :: function< ...>
,例如:
Especially when the functions being called need to be compiled separately, using a template for each kind of function object isn't viable. In that case it is much more reasonable to use a type-erased representation, i.e., std::function<...>
, e.g.:
double fptr_test(std::function<double(double)> fun, double input) {
return fun(input);
}
函数对象只需要一个参数,而你的成员函数有两个:调用函数on的对象和 double
参数。你可以 std :: bind(...)
第一个参数传递给 fptr_test()
:
In both cases the function object takes just one argument while your member function takes two: the object to call the function on and the double
argument. You'd std::bind(...)
the first argument to an object and pass the resulting object to fptr_test()
:
test object;
std::cout << fptr_test(std:bind(&test::tt, &object, std::placeholders::_1), 3) << '\n';
std::cout << fptr_test([&](double input){ return object.tt(input); }, 3) << '\n';
代码使用两种单独的方法绑定对象:第一种使用 std :: bind()
,而第二个使用lambda函数。这两个调用都应该同时执行 fptr_test()
。
The code uses two separate approaches to bind the object: the first uses std::bind()
while the second uses a lambda function. Both of these calls should work with both of the implementation of fptr_test()
provided.
这篇关于函数指向类成员函数的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!