简体   繁体   English

C ++函数指针语法

[英]C++ function pointer syntax

I know that generally, a function pointer is declared like this: 我知道通常,函数指针声明如下:

 void __stdcall my_func(int i) {} // for assigning

 void (__stdcall * my_ptr)(int) = &my_func;

Now I had several functions which take a function pointer as their argument: 现在我有几个函数将函数指针作为它们的参数:

 void calling_func(void (__stdcall * callback)(int)) { *callback(1); }

In the header file, I just removed the name of the thing: 在头文件中,我刚刚删除了该东西的名称:

void calling_func(void (__stdcall *)(int));

and voilá, that worked ... and got me thinking: If that is a complete type declaration, couldn't one use the normal TYPE NAME = VALUE syntax also for function pointers? 和voilá,有用......让我思考:如果那是一个完整的类型声明,那么对于函数指针也不能使用普通的TYPE NAME = VALUE语法吗?

I tried to declare: 我试图宣布:

 ( void (__stdcall *)(int) ) callback = &my_func;

and it also compiles! 它也编译! Why isn't this used more often? 为什么不经常使用这个? Are there flaws with this notation? 这种符号有缺陷吗? To me it seems that it would greatly ease the use of function pointers ... also in typedefs, for example: generally it's typedef OLDNAME NEWNAME , just with function pointers it's this weird inside-out syntax where the new name is actually in the middle of the whole expression. 对我而言,它似乎会大大简化函数指针的使用......也可以在typedef中使用,例如:通常它是typedef OLDNAME NEWNAME ,只是使用函数指针,这是一种奇怪的由内而外的语法,其中新名称实际上在中间整个表达。 Let alone the notation for functions returning a function pointer ... 更不用说返回函数指针的函数的符号了......

Doesn't compile for me. 不为我编译。 AFAIK it's not valid C++. AFAIK它不是有效的C ++。

You can fake it, though! 不过你可以伪造它!

template <typename T>
struct identity {
   typedef T type;
};

identity<void(*)(int)>::type callback = &my_func;

Or use something like boost::function (or std::function ) for the ultimate wins. 或者使用boost::function (或std::function )之类的东西来获得终极胜利。

That is not a declaration, it's a cast of callback , which is then assigned to. 这不是一个声明,它是一个callback演员,然后分配给。

Are you sure you didn't have a pre-existing declaration in scope? 您确定您的范围内没有预先存在的声明吗?

I much prefer typedef ing my function pointers, and have yet to have problems with it. 我更喜欢使用typedef我的函数指针,并且还没有遇到问题。

Typedef, if I understand it correctly, doesn't so much make a new type as an alias to the other one (which is why size_t and unsigned int can be used interchangeably without compiler complaints, for example). 如果我理解正确的话,Typedef不会将新类型作为另一个的别名(这就是为什么size_tunsigned int可以互换使用而不需要编译器投诉)。 So, for function pointer types you use often, typedefs make them a lot easier to work with. 因此,对于经常使用的函数指针类型,typedef使它们更容易使用。

For function pointer typedefs, the syntax goes: 对于函数指针typedef,语法为:

typedef IObject *    (*CreateFunc )(int, Core *);

With the desired type name on the inside (not much different from what you're doing now). 在内部使用所需的类型名称(与您现在正在进行的操作没有太大区别)。 You can specify most function modifiers (__stdcall, __declspec, etc), they come just inside the parentheses before the * name . 您可以指定大多数函数修饰符(__stdcall,__ declspec等),它们只是在* name之前的括号内。

You can call, assign and get the value of these quite easily, as well as converting them to void * for passing (when the type is unknown). 您可以非常轻松地调用,分配和获取这些值,并将它们转换为void *以便传递(当类型未知时)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM