如何将函数指针传递到c++中的函数
How to pass function-pointer to a function in c++?
这里有三个函数,例如:-
float Plus (float a, float b) { return a+b; }
float Minus (float a, float b) { return a-b; }
float Multiply(float a, float b) { return a*b; }
现在有一个函数将指向函数的指针作为参数之一:-
void Function_Pointer_func(float a, float b, float (*pt2Func)(float, float))
{
float result = pt2Func(a, b); // call using function pointer
cout << " Result = "; // display result
cout << result << endl;
}
要调用上面的函数"function_Pointer_func",该函数写在下面
void Replace()
{
Function_Pointer_func(2, 5, /* pointer to function 'Minus' */ Plus);//// (1)
Function_Pointer_func(2, 5, /* pointer to function 'Minus' */ &Minus);//// (2)
}
为什么函数"function_Pointer_func"以函数指针为参数,上面的函数工作得很好。如果我们更换中的RHS
float result = pt2Func(a, b); // call using function pointer
函数"function_Pointer_func"的(*pt2Func)(a,b);那么它也适用于(&pt2Func)(a,b);
它在VS2008:中给出了一个错误
"错误C2064:术语的计算结果不是采用2个参数的函数"
现在,将函数"function_Pointer_func"中的"float(*pt2Func)(float,float)"参数替换为float(pt2Func)(float-floot),然后将所有三个
float result = pt2Func(a, b); //
float result = (&pt2Func)(a, b); //
float result = (*pt2Func)(a, b); //
声明有效,为什么?我希望我不舒服的原因在于理解了函数指针的核心理解。好吧,我不是在介绍Q?没有任何好的阅读量,但是的,我没有对此进行任何深入的研究,所以请随时推荐一些这方面的阅读,以解决我的歧义。
感谢您提前提供的帮助。
函数会自动衰减为函数指针。在这种情况下,
-
如果未指定,则
function_name
实际上意味着&function_name
。 -
&function_name
将函数转换为函数指针。 -
*function_name
实际上是指*(function_name)
,根据上面的内容,它变成了*(&function_name)
。可以说,*
和&
"抵消",由此产生的function_name
衰减回&function_name
。
它是c++标准。
float Plus(float a, float b);
void Function_Pointer_func(float a, float b, float (*pt2Func)(float, float));
Function_Pointer_func(2, 5, Plus); // (1)
...
float result = pt2Func(a, b); // (2)
(1)是函数到指针的转换(标准2003,4.3):
An lvalue of function type T can be converted to an rvalue of
type “pointer to T.” The result is a pointer to the function
(2)是函数调用(标准2003,5.2.2):
For an ordinary function call, the postfix expression shall be either
an lvalue that refers to a function (in which case the function-to-pointer
standard conversion (4.3) is suppressed on the postfix expression), or it
shall have pointer to function type.
[更新]详细信息:
void Replace() {
Function_Pointer_func(2, 5, Plus);
Function_Pointer_func(2, 5, &Minus);
}
减号是函数=>&Minus是指向函数的指针,因此没有转换,第三个参数Function_Pointer_func的完全匹配Plus是一个函数,因此要适应function_Pointer_func,必须将其转换为指针。标准(1)说它可以自动完成。
呼叫案例:
void Function_Pointer_func(float a, float b, float (*pt2Func)(float, float)) {
float result = pt2Func(a, b); // call by pointer, see (2)
float result = (*pt2Func)(a, b); // convert pointer to function, so 'normal' call
float result = (&pt2Func)(a, b); // pointer to function pointer, nope, will not work
}
- "error: no matching function for call to"构造函数错误
- 什么时候调用组成单元对象的析构函数
- 继承函数的重载解析
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- C++模板来检查友元函数的存在
- 递归函数计算序列中的平方和(并输出过程)
- 对RValue对象调用的LValue ref限定成员函数
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 在C++STL中是否有Polyval(Matlab函数)等价物?
- 为什么使用 "this" 指针调用派生成员函数?
- 将对象数组的引用传递给函数
- 函数调用中参数的顺序重要吗
- 函数向量_指针有不同的原型,我可以构建一个吗
- 使用不带参数的函数访问结构元素
- 代码在main()中运行,但在函数中出现错误
- 内置函数可查看CPP中的成员变量
- 如何获取std::result_of函数的返回类型
- 如何在c++中为模板函数实例创建快捷方式
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗