雖然共同空間定義許多資料型態
但這些資料型態卻不能同時存在
新加入的資料將會把舊的資料給覆蓋
跟結構一樣
可以使用點運算子(.)來存取內容
Ex:
#include <iostream> using namespace std; union data { int math; char gender; }; int main(){ union data d1; d1.gender='M'; //將變數內容設為M cout << "gender is " << d1.gender << endl; d1.math=70; //將變數內容設為70 cout << "math grade is " << d1.math << endl; }