[英]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
以及如何理解? for
循環中的int_array
是什么類型?
謝謝
兩者都在做完全相同的事情:Defining int_array
as an alias of an array of 4 int
s
using
有一個很好的 A = B 符號,通常更容易理解。
using alias = type;
typedef
的符號並不是很落后。 對於一個簡單的typedef
typedef type alias;
但是更復雜的typedef
往往會蔓延。 我懷疑語法是根據人們定義變量的方式建模的,但我找不到我把舊的 K&R C 編程書的副本打包在哪里,目前無法查找。
int int_array[4];
會將int_array
定義為 4 個int
的數組。 在前面拍打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, ...);
如果你想要一個二維數組,你可以
using int_array2D = int[4][4];
或者你可以定義一個 int_array 數組
using int_array2D = int_array[4];
當然這意味着你可以
using int_array3D = int_array2D[4];
繼續前進,直到奶牛回家,或者你已經擠滿了太多維度,以至於醫生的大腦都融化了。
兩種類型別名相同:
類型別名,別名模板 (C++11 起) :
類型別名是引用先前定義的類型的名稱(類似於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
進行編譯/鏈接。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.