您现在的位置: 天极网 > 开发频道 > C++中union的应用剖析
全文

C++中union的应用剖析

2001-11-21 08:11作者:袁小凯出处:yesky责任编辑:方舟

  二、类中union的初始化

  由于union的共享内存特点,我们可以使我们的类存储不同的型别而不浪费内存空间,在类中我们可以声明一个union存储不同型别的指针,示例如下:

#pragma warning(disable : 4786)
#include

using namespace std;

class TestUnion
{
enum StoreType{Long,Const_CharP};
union
{
const char* ch_;
long l_;
} data_;
StoreType stype_;
TestUnion(TestUnion&);
TestUnion& operator=(const TestUnion&);
public:
TestUnion(const char* ch);
TestUnion(long l);
operator const char*() const {return data_.ch_;}
operator long() const {return data_.l_;}
};

TestUnion::TestUnion(const char* ch):data_.ch_(ch),stype_(Const_CharP)
{
}

TestUnion::TestUnion(long l):data_.l_(l),stype_(Long)
{
}

int main (void)
{
TestUnion pszobj("yuankai");
TestUnion lobj(1234);
cout<(pszobj)< cout<
return 0;
}

共3页。 9 1 2 3 :

软件资讯·软件下载尽在天极软件

相关搜索:
相关文章及软件
关注此文读者还看过
热门关注
特别推荐
网友关注
软件下载
娱乐下载
驱动下载
文章排行
本周
本月
最近更新
关于我们|About us|网站律师|天极服务|电子杂志|RSS订阅|加入我们|网站地图
TMG
Copyright (C) 1999-2009 Chinabyte.com, All Rights Reserved 版权所有 天极网络
商务联系、网站内容、合作建议:010-82657868
版权声明 在线提交意见反馈 渝ICP证B2-20030003号
经营性网站备案信息 网警备案 中国网站排名
天极传媒:天极网|比特网|IT专家网|IT商网|52PK游戏网|IT分众