如何读取函数指针
How to read the function pointer?
如何解释下面的 c++ 声明?
int (*(*x[2])())[3];
这是来自类型cpp首选项中的示例。
问题中示例声明的解释存在于链接的 Type-cpp首选项页面中。
int (*(*x[2])())[3]; // declaration of an array of 2 pointers to functions
// returning pointer to array of 3 int
因此,实际的问题不是关于那个案例,而是关于如何解读任何C++声明。
您可以推断所有这些详细信息,从声明的名称开始,x
并顺时针移动括号。您将得到上面的描述:
x 是一个指向函数的指针的第 2 维数组,该函数返回指向整数的维数 3 的数组的指针。
这里更好地解释为顺时针/螺旋规则:http://c-faq.com/decl/spiral.anderson.html
它是一个由
两个指针组成的数组,指向返回指向类型为 int[3]
且没有参数的数组的指针。
这是一个演示程序
int ( *f() )[3] { return new int[2][3]; }
int ( *g() )[3] { return new int[4][3]; }
int main()
{
int (*(*x[2])())[3] = { f, g };
for ( auto func : x ) delete []func();
return 0;
}
相关文章:
- QMetaObject invokeMethod的基于函数指针的语法
- C++-试图将函数指针推回到另一个CPP文件中的矢量时出错
- c++r值引用应用于函数指针
- 模板函数指针和lambda
- 是否可以将llvm::FunctionType转换为C/C++原始函数指针
- 带有类的函数指针
- () 函子后面的括号,而不是函数指针?
- 全局作用域中函数指针的赋值
- 使用"Task"函数指针队列定义作业管理器
- 将成员函数指针作为参数传递给模板方法
- 如何创建对象函数指针C++映射?
- 匹配函数指针作为模板参数?
- 通过函数指针定义类范围之外的方法
- 存储在类中的函数指针
- C++从函数指针数组调用函数
- 将返回值存储在函数指针数组的指针中是如何工作的?
- 整数键映射到头文件中的成员函数指针
- 从类成员函数到类 C 函数指针的转换
- 如何将内联匿名函数分配给C++函数指针
- 将字符缓冲区强制转换为函数指针