聯合體(union)
資料型別的一種, 允許您在相同的記憶體位置存盤不同的資料型別,您可以定義一個帶有多成員的共用體,但是任何時候只能有一個成員帶有值,共用體提供了一種使用相同的記憶體位置的有效方式,
一般應用場景為節省記憶體,
定義
union A
{
char i;
int j;
double k;
}b;
訪問
int main() {
ios::sync_with_stdio(false);
b.i = 'a';
cout << b.i << endl;
cout << b.j << endl;
cout << b.k << endl;
cout << endl;
b.j = 10;
cout << b.i << endl;
cout << b.j << endl;
cout << b.k << endl;
cout << endl;
b.k = 1.22342;
cout << b.i << endl;
cout << b.j << endl;
cout << b.k << endl;
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/68340.html
標籤:其他
