//学习 联合: 联合不能包含带有构造函数或析构函数的成员,因为无法保护其中对象以防止破坏,
//也不能保证在联合离开作用域时能调用正确的析构函数。
/******************************************************************************************
#include "stdafx.h"
#include <string>
#include <fstream>
#include <iostream>
#include <algorithm>
#include <map>
#include <vector>

using namespace std;

int _tmain(int argc,_TCHAR* argv[])
{
 //定义联合类型
 union union_1  {
  char    ccc;
  int     kkk;
  float   xxx;
 };

 //声明联合变量
 union union_1 uuu;

 // 使用联合变量中的字符型成员
 uuu.ccc = '*';
 cout << uuu.ccc << endl;//运行结果:*

 // 使用联合变量中的整型成员
 uuu.kkk = 1000;
 cout << uuu.kkk << endl;//运行结果:1000

 // 使用联合变量中的浮点型成员
 uuu.xxx = 3.1416f;
 cout << uuu.xxx << endl;//运行结果:3.1416

 //声明联合变量时初始化
 union_1 uuu1 = {'A'};

 //同时引用联合变量的各成员
 cout << uuu1.ccc << endl;//运行结果:A
 cout << uuu1.kkk << endl;//运行结果:65
 cout << uuu1.xxx << endl;//???运行结果:9.10844e-044
 return 0;
}

posted on 2008-04-08 23:47 -274°C 阅读(583) 评论(0)  编辑  收藏 所属分类: C++

只有注册用户登录后才能发表评论。


网站导航:
 

常用链接

留言簿(21)

随笔分类(265)

随笔档案(242)

相册

JAVA网站

关注的Blog

搜索

  •  

积分与排名

  • 积分 - 909090
  • 排名 - 40

最新评论