如何在不取消引用的情况下调用函数指针? [英] How come pointer to a function be called without dereferencing?
问题描述
我在Py ++生成的C ++程序中有一个奇怪的typedef语句。
I have a weird typedef statement in a C++ program, generated by Py++.
double radius(int); // function to be wrapped
typedef double (*radius_function_type)(int);
bp::def("radius", radius_function_type(&radius)); // bp::def is a function for wrapping
的函数typedef statemnt不是这种类型,我们大多数人都很熟悉。
What I figured out so far is that the above typedef statemnt is not of the type, most of us are familiar with,
typedef complex_type simple_alias;
这是一种声明指向函数的指针的方法,该函数将int作为参数并返回double(相同作为原型)。因此,我现在的问题是,如何以函数的地址作为参数(不进行取消引用)?这也与原型不符。有人请解释!
Rather it is a way to declare pointer to a function which takes int as argument and returns double (same as the prototype). So my question now is that, how come pointer to a function (without dereferencing) be called with address of a function as an argument? This also doesn't match with the prototype. Somebody please explain!
推荐答案
您的问题令人困惑。您是否在问:
Your question is confusing. Are you asking what this does:
radius_function_type(&radius)"
这只是C ++类型转换,有点像:
This is just a C++ typecast, a bit like:
radius (int (42));
但由于半径已经是radius_function_type类型,因此您可以一样容易做到:
but since radius is already of type radius_function_type then you can just as easily do:
bp::def("radius", radius);
但是由于这是Py ++生成的代码,因此输出时可能要格外小心。
but as this is code generated by Py++, it's probably being extra careful with the output.
这篇关于如何在不取消引用的情况下调用函数指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!