C++用using定义指向数组的指针类型别名及语法对比
C++中using与typedef定义数组相关指针类型别名的对比
1. 用using定义与ptr_arr等价的类型别名
对应typedef int (*ptr_arr)[];的using写法如下,两者完全等价:
using ptr_arr = int (*)[]; // 指向数组的指针类型别名
2. 指向数组的指针与指针数组的类型别名定义对比
指向数组的指针(Pointer to array)
这是一个指向整个数组的指针类型:
- typedef 写法:
typedef int (*ptr_to_arr)[]; - using 写法:
using ptr_to_arr = int (*)[];
指针数组(Array of pointers)
这是一个元素为指针的数组类型:
- typedef 写法:
typedef int* arr_of_ptr[]; - using 写法:
using arr_of_ptr = int*[];
语法差异说明
- typedef 采用声明式语法,需要把类型别名嵌入到类型声明的位置,对于复杂类型(比如指向数组的指针),需额外注意括号位置以避免歧义。
- using 采用赋值式语法,结构更直观:
using 别名 = 目标类型;,无论类型复杂度如何,语法结构保持一致,可读性更强。
内容的提问来源于stack exchange,提问作者int main
相关产品推荐
相关产品推荐

