Using c++ typedef/using type alias
Using c++ typedef/using type alias
我正在阅读C++入门书,完全听不懂一行:
using int_array = int[4];
typedef int int_array[4]; // This line
for (int_array *p = ia; p != ia + 3; ++p) {
for (int *q = *p; q != *p + 4; ++q)
cout << *q << ' '; cout << endl;
}
好的typedef
和using
一样。这是否意味着int[4][4]
现在int
以及如何理解这一点?循环中int_array
for
什么类型?
谢谢
TL;博士
两者都在做完全相同的事情:将int_array
定义为 4int
s 数组的别名
理解语法
using
有一个很好的A = B表示法,通常更容易理解。
using alias = type;
typedef
的记谱法并不完全落后。对于一个简单的typedef
typedef type alias;
但更复杂的typedef
往往会蔓延。我怀疑语法是根据定义变量的方式建模的,但我找不到我在哪里打包我的旧K&R C编程书,目前也无法查找。
int int_array[4];
将int_array
定义为 4int
s 的数组。 拍打typedef
typedef int int_array[4];
使int_array
成为类型别名而不是变量。
再比如,
int * intp;
将intp
定义为指向int
的指针。
typedef int * intp;
将intp
定义为指向int
的类型指针的别名。
对于更复杂的数据类型,这变得很丑陋,因为typedef
ed别名的名称可能隐藏在定义中间的某个地方。typedef
ed 函数指针,例如:
typedef void (*funcp)(param_t param1, param_t param2, ...);
与使用
using funcp = void (*)(param_t param1, param_t param2, ...);
制作 2D 阵列
如果你想要一个2D阵列,你可以
using int_array2D = int[4][4];
或者您可以定义一个int_array数组
using int_array2D = int_array[4];
当然,这意味着您可以
using int_array3D = int_array2D[4];
继续前进,直到奶牛回家,或者你已经挤满了如此多的维度,以至于医生的大脑融化了。
此行不执行任何操作,因为它是多余的
线条
using int_array = int[4];
和
typedef int int_array[4];
做同样的事情。有关使用和类型定义,请参阅参考。你可以省略一个或另一个,行为是相同的。有两个不同的声明不是错误,因为它们并不冲突(它们执行完全相同的事情(。
第一种方法(使用using
关键字(是在 C++11 中引入的,在我看来更容易阅读,所以我更喜欢它而不是 typedef 版本。
两种类型别名相同:
Type alias,aliastemplate(自 C++11 起(:
Type alias 是一个名称,指的是以前定义的类型(类似于 typedef(:
using identifier attr(optional) = type-id ;
<小时 />所以你可以使用:
typedef int int_array[4];
或者你可以只使用(与上面相同(:
using int_array = int[4];
当您需要使用4*sizeof(int)
步骤寻址内存时,例如,如果系统int
大小为 4 字节,则内存步长大小为 4*4=16 字节。 在这种情况下,即使您可以使用int_array *p;
++p
p
前进一个内存步长,例如 16 字节。 看:
1- 带using int_array = int[4];
的工作样品:
#include <iostream>
using std::cout; using std::endl;
int main()
{
int ia[3][4] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
// a range for to manage the iteration
// use type alias
using int_array = int[4];
for (int_array& p : ia)
for (int q : p)
cout << q << " ";
cout << endl;
// ordinary for loop using subscripts
for (size_t i = 0; i != 3; ++i)
for (size_t j = 0; j != 4; ++j)
cout << ia[i][j] << " ";
cout << endl;
// using pointers.
// use type alias
for (int_array* p = ia; p != ia + 3; ++p)
for (int *q = *p; q != *p + 4; ++q)
cout << *q << " ";
cout << endl;
return 0;
}
输出 1:
0 1 2 3 4 5 6 7 8 9 10 11
0 1 2 3 4 5 6 7 8 9 10 11
0 1 2 3 4 5 6 7 8 9 10 11
2- 使用typedef int int_array[4];
的工作样品:
#include <iostream>
using std::cout; using std::endl;
int main()
{
int ia[3][4] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
// a range for to manage the iteration
// use type alias
typedef int int_array[4];
for (int_array& p : ia)
for (int q : p)
cout << q << " ";
cout << endl;
// ordinary for loop using subscripts
for (size_t i = 0; i != 3; ++i)
for (size_t j = 0; j != 4; ++j)
cout << ia[i][j] << " ";
cout << endl;
// using pointers.
// use type alias
for (int_array* p = ia; p != ia + 3; ++p)
for (int *q = *p; q != *p + 4; ++q)
cout << *q << " ";
cout << endl;
return 0;
}
输出 2(相同(:
0 1 2 3 4 5 6 7 8 9 10 11
0 1 2 3 4 5 6 7 8 9 10 11
0 1 2 3 4 5 6 7 8 9 10 11
参考: https://github.com/Mooophy/Cpp-Primer/blob/master/ch03/ex3_44.cpp
注意:使用-std=c++11
进行编译/链接。
- 使用 [] 运算符时"binding reference of type discards qualifiers"
- 在 QVector<std::unique_ptr 上使用 std::find<Type>>
- 在类型和包装器之间reinterpret_cast是否安全<Type>?
- 使用 cmake 的 Linux 终端上的"Conversion to non-scalar type is requested"错误
- 控制到达非空函数clang(-Wreturn-type)的末尾
- std::unordered_map 类型对象声明期间出现"field has incomplete type"错误
- 将系数存储在头文件的数组中("does not name a type"错误)
- 尝试打开 ifstream 时出现"Incomplete type"错误
- 将成员函数作为构造函数参数调用时出错 "Variable is not a type name"
- 在"结构提升::enable_if<提升::is_pod<T>,无效>"中没有名为"type"的类型
- "Missing type specifier - int assumed"无法通过向主函数添加"return 0"来解决
- OpenCV CV_16F type
- C++ "错误:在'类 std::result_of< ... >"中没有名为'type'的类型"
- 将内存分配返回值强制转换为 TYPE 数组
- C++ 一个lambda浅拷贝const Type&如果它被赋予一个命名捕获,如[copy=val](){}?
- reference_wrapper导致"incomplete type is not allowed"
- 继承类时"invalid use of incomplete type ‘class tree_node_t’"
- 由于"error C4430: missing type specifier - int assumed. Note: C++ does not support default-int",我现在无法编
- Using c++ typedef/using type alias
- Clang 对使用的类型别名发出"unused type alias"警告