//學(xué)習(xí) 聯(lián)合: 聯(lián)合不能包含帶有構(gòu)造函數(shù)或析構(gòu)函數(shù)的成員,因?yàn)闊o法保護(hù)其中對(duì)象以防止破壞,
//也不能保證在聯(lián)合離開作用域時(shí)能調(diào)用正確的析構(gòu)函數(shù)。
/******************************************************************************************
#include "stdafx.h"
#include <string>
#include <fstream>
#include <iostream>
#include <algorithm>
#include <map>
#include <vector>
using namespace std;
int _tmain(int argc,_TCHAR* argv[])
{
?//定義聯(lián)合類型
?union union_1? {
??char??? ccc;
??int???? kkk;
??float?? xxx;
?};
?//聲明聯(lián)合變量
?union union_1 uuu;
?// 使用聯(lián)合變量中的字符型成員
?uuu.ccc = '*';
?cout << uuu.ccc << endl;//運(yùn)行結(jié)果:*
?// 使用聯(lián)合變量中的整型成員
?uuu.kkk = 1000;
?cout << uuu.kkk << endl;//運(yùn)行結(jié)果:1000
?// 使用聯(lián)合變量中的浮點(diǎn)型成員
?uuu.xxx = 3.1416f;
?cout << uuu.xxx << endl;//運(yùn)行結(jié)果:3.1416
?//聲明聯(lián)合變量時(shí)初始化
?union_1 uuu1 = {'A'};
?//同時(shí)引用聯(lián)合變量的各成員
?cout << uuu1.ccc << endl;//運(yùn)行結(jié)果:A
?cout << uuu1.kkk << endl;//運(yùn)行結(jié)果:65
?cout << uuu1.xxx << endl;//???運(yùn)行結(jié)果:9.10844e-044
?return 0;
}
posted on 2008-04-08 23:47
-274°C 閱讀(593)
評(píng)論(0) 編輯 收藏 所屬分類:
C++