LxL*_*LxL 3 c++ function-pointers function
在标准的章节§6.8(N3690草案)中,我看到了这段奇怪的代码:
struct T2 { T2(int){ } };
int a, (*(*b)(T2))(int), c, d;
Run Code Online (Sandbox Code Playgroud)
什么是 int(*(*b)(T2))(int)?!
是b指向T2构造函数的指针吗?或者是指向函数指针的指针?
波纹管代码也编译得很好奇怪!:
struct T2 { T2(int){ } };
int((*b)(T2));
Run Code Online (Sandbox Code Playgroud)
int (*(*b)(T2))(int)
Run Code Online (Sandbox Code Playgroud)
它声明b为指向函数的指针:
T2作为参数int参数int.应该使用typedef简化此声明:
typedef int (*return_type) (int);
typedef return_type(*function_type) (T2);
Run Code Online (Sandbox Code Playgroud)
或者更好地使用C++ 11样式类型别名:
using return_type = int(*)(int);
using function_type = return_type(*)(T2);
Run Code Online (Sandbox Code Playgroud)
然后宣言成为:
function_type b; //so simple!
Run Code Online (Sandbox Code Playgroud)
希望有所帮助.
| 归档时间: |
|
| 查看次数: |
104 次 |
| 最近记录: |