加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 找出模糊指针typedef

发布时间:2020-12-16 06:45:38 所属栏目:百科 来源:网络整理
导读:typedef solution_type (*algorithm_ptr_type) ( problem_type problem,void (*post_evaluation_callback)(void *move,int score)/* = NULL*/); 请帮我!谢谢 解决方法 这意味着,algorithm_ptr_type是一个指向返回solution_type的函数的指针,其参数为: 类型
typedef solution_type (*algorithm_ptr_type) (
    problem_type problem,void (*post_evaluation_callback)(void *move,int score)/* = NULL*/
);

请帮我!谢谢

解决方法

这意味着,algorithm_ptr_type是一个指向返回solution_type的函数的指针,其参数为:

>类型problem_type的问题
> post_evaluation_callback,它又是一个带有两个参数(void *和int)并返回void的函数指针.

同样可以写成(简单易读的语法):

typedef  void (*callback_type)(void *move,int score);

typedef solution_type (*algorithm_type)(problem_type,callback_type);

注意:参数的名称是可选的,所以我删除它,使typedef简短可爱!

在C 11中,这可以进一步简化如下:

using algorithm_ptr_type = solution_type (*) (
    problem_type,void(*)(void*,int)
);

这样做要好得多,因为现在已明确定义了什么以及什么.

在C 11中,您甚至可以定义一个实用程序来创建函数指针,

//first define a utility to make function pointer.
template<typename Return,typename ... Parameters>
using make_fn = Return (*)(Paramaters...);

然后用它,

using callback_type = make_fn<void,void*,int>;

using algorithm_type = make_fn<solution_type,problem_type,callback_type>;

这里make_fn的第一个参数是返回类型,其余的是参数 – 很容易解读每个参数!

用法:

solution_type SomeFunction(problem_type problem,callback post_evaluation)
{
   //implementation

   //call the callback function
   post_evaluation(arg1,arg2);
   //..
}

algorithm_ptr_type function = SomeFunction;

//call the function
function(arg,someOtherFunction);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读