回调函数:void(* func)(int)和void(func)(int) [英] callback function: difference between void(*func)(int) and void(func)(int)
问题描述
所以我们说我有一个函数:
So Lets say I have a function:
void foo (int i){
cout << "argument is: " << i << endl;
}
我将这个函数传递给:
void function1 (void(callback)(int), int arg){
callback(arg);
}
void function2 (void(*callback)(int), int arg){
callback(arg);
}
这两个功能是否相同?
are these two functions identical? Is there any difference between the two?
推荐答案
规则是在函数的参数列表中声明一个函数类型被调整为具有指向函数类型的指针(类似地,并且可能更加公知,被声明为具有类型 T
的参数被调整为具有类型指针 T
。
The rule is that in a function's parameter list, a parameter declared to have a function type is adjusted to have pointer to function type (similarly, and probably more well-known, a parameter declared to have type "array of T
" is adjusted to have type "pointer to T
". Redundant parentheses in declarators are allowed, but ignored.
因此,在
void function1 (void(callback)(int), int arg);
void function2 (void (*callback)(int), int arg);
void function3 (void callback(int), int arg);
这三个函数的第一个参数具有完全相同的类型 - (int)的函数指针
返回 void
。
The first parameter of those three functions have exactly the same type - "pointer to function of (int)
returning void
".
这篇关于回调函数:void(* func)(int)和void(func)(int)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!