typedef solution_type (*algorithm_ptr_type) (
problem_type problem,
void (*post_evaluation_callback)(void *move, int score)/* = NULL*/
);
Run Code Online (Sandbox Code Playgroud)
请帮我!谢谢
Naw*_*waz 16
这意味着,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);
Run Code Online (Sandbox Code Playgroud)
注意:参数的名称是可选的,所以我删除它,使typedef简短可爱!
在C++ 11中,这可以进一步简化如下:
using algorithm_ptr_type = solution_type (*) (
problem_type,
void(*)(void*, int)
);
Run Code Online (Sandbox Code Playgroud)
这样做要好得多,因为现在已明确定义了什么以及什么.
在C++ 11中,您甚至可以定义一个实用程序来创建函数指针,
//first define a utility to make function pointer.
template<typename Return, typename ... Parameters>
using make_fn = Return (*)(Paramaters...);
Run Code Online (Sandbox Code Playgroud)
然后用它,
using callback_type = make_fn<void, void*, int>;
using algorithm_type = make_fn<solution_type, problem_type, callback_type>;
Run Code Online (Sandbox Code Playgroud)
这里的第一个参数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);
Run Code Online (Sandbox Code Playgroud)
多么可怕的代码!
它的作用是定义一个被调用的函数指针类型algorithm_ptr_type,返回a solution_type并将a problem_type作为其第一个arg,将一个回调作为其第二个arg.回调接受void*并int作为其args并且不返回任何内容.
写这个的更好方法是:
typedef void (*post_evaluation_callback)(void *move, int score);
typedef solution_type (*algorithm_ptr_type)(problem_type problem, post_evaluation_callback callback);
Run Code Online (Sandbox Code Playgroud)