回调函数:void(* func)(int)和void(func)(int) [英] callback function: difference between void(*func)(int) and void(func)(int)

查看:508
本文介绍了回调函数:void(* func)(int)和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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆