diz*_*cza 9 c typedef declaration
typedef float vec3[3];
void test(vec3 const vptr) {
*vptr = 1.f; // error: assignment of read-only location
vptr[0] = 1.f; // error: assignment of read-only location
vptr++; // no error
}
Run Code Online (Sandbox Code Playgroud)
是
vec3 const vptr
Run Code Online (Sandbox Code Playgroud)
同样的
const vec3 vptr
Run Code Online (Sandbox Code Playgroud)
对于所有typedef?最后两个之间有什么区别吗?我想
vec3 const vptr <==> float* const vptr // a constant pointer to an object
const vec3 vptr <==> const float* vptr // a pointer to a constant object
??? <==> const float* const vptr // a constant pointer to a constant object
Run Code Online (Sandbox Code Playgroud)
这个类型定义
typedef float vec3[3];
Run Code Online (Sandbox Code Playgroud)
定义数组类型的别名float[3]
这个参数的声明
vec3 const vptr
Run Code Online (Sandbox Code Playgroud)
声明vptr为具有数组类型const float[3]。
指定为数组类型的函数参数将调整为指向数组元素类型的对象的指针。
所以这个声明
vec3 const vptr
Run Code Online (Sandbox Code Playgroud)
调整为类型const float *vptr。也就是说,它是一个指向常量对象的非常量指针。
这个关系
vec3 const vptr <==> float* const vptr // 指向对象的常量指针
是错的。还有这个声明
vptr++; // no error
Run Code Online (Sandbox Code Playgroud)
证实了这一点。
您无法获得此声明
const float* const vptr
Run Code Online (Sandbox Code Playgroud)
使用这个类型定义
typedef float vec3[3];
Run Code Online (Sandbox Code Playgroud)