一区二区国产高清视频在线_真人性做爰A片免费_强伦人妻BD在线电影_亚洲熟妇无码AV_免费人成视频在线观看网站_亚洲欧美精品午睡沙发_在线观看无码的免费网站_中文字幕无码A片久久_日韩欧美国产一区精品_久久精品女人天堂av

重慶醫(yī)科大學(xué)論壇

標(biāo)題: C++中union的應(yīng)用剖析 [打印本頁]

作者: liwei58    時間: 2007-11-23 09:36
標(biāo)題: C++中union的應(yīng)用剖析
前言

  熟悉C的程序員都知道union(聯(lián)合體)的用法,利用union可以用相同的存儲空間存儲不同型別的數(shù)據(jù)類型,從而節(jié)省內(nèi)存空間。當(dāng)訪問其內(nèi)成員時可用"."和"->"來直接訪問。在C++出現(xiàn)后,它繼承了union并保留了其在C中的特性。但是在C++中的union又有了新的擴(kuò)展,這需要大家了解,要不然你會感到費(fèi)解和迷惑。下面我講兩點(diǎn)。

  一、在union中存儲對象

  在C中union中可以存儲任意類型的內(nèi)置數(shù)據(jù)類型,那么在C++中union是否可以存儲對象呢?還是讓我們看一個例子吧,這比任何言語都能說明問題,不是嗎?

#pragma warning(disable : 4786)
#include
using namespace std;

class TestUnion
{
 public:
 TestUnion(long l):data_(l)
 {
  };
 int data_;
};

typedef union _tagUtype_
{
 TestUnion obj;
}UT;

int main (void)
{
 return 0;
}


  這樣不行,union中不可以存儲TestUnion類的對象,但在C中union可以存儲struct呀,為什么不能存儲類的對象呢?很簡單,請問,在C中union可以存儲帶有構(gòu)造函數(shù)的struct嗎?對了,在C中的struct是沒有構(gòu)造函數(shù)的。所以如果C++中union可以存儲有構(gòu)造函數(shù)的類的對象就不太符合邏輯,那不是說C++和C完全兼容嗎?不錯,正因?yàn)檫@一點(diǎn),C++中union不可以存儲有構(gòu)造函數(shù)的類的對象,但是可以存儲不帶構(gòu)造函數(shù)的類的對象,這樣就和C保持一致了,不想信你試試。對TestUnion類的聲明進(jìn)行如下修改:

class TestUnion
{
 public:
 int data_;
};

  再進(jìn)行編譯,一切OK!。但是這樣卻失去了C++的構(gòu)造初始化特性,這樣做是沒有任何意義的,我只是在說其在C++中的語義,并不是推薦大家使用(絕對不推薦)。但是我們可以在union中存儲對象的指針,從而引用不同的對象類型。不用我再多說了吧,大家還是試試吧!

  二、類中union的初始化

  由于union的共享內(nèi)存特點(diǎn),我們可以使我們的類存儲不同的型別而不浪費(fèi)內(nèi)存空間,在類中我們可以聲明一個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<<STATIC_CAST(pszobj)<<ENDL;
cout<<LOBJ<<ENDL;

return 0;
}


  真是不幸,編譯都通不過,好象沒有什么問題呀,為什么呢?data_.ch_(ch)和data_.l_(l)有問題嗎?如果你問一個C程序員他會告訴你,絕對沒問題。你不會去懷疑編譯器有問題吧!不好意思!我一開始就是這么想的,真是慚愧。費(fèi)解,迷惑。讓我們來看看構(gòu)造TestUnion對象時發(fā)生了什么,這樣你就會明白了。當(dāng)創(chuàng)建TestUnion對象時,自然要調(diào)用其相應(yīng)的構(gòu)造函數(shù),在構(gòu)造函數(shù)中當(dāng)然要調(diào)用其成員的構(gòu)造函數(shù),所以其要去調(diào)用union成員的構(gòu)造函數(shù),但是其為匿名的,有沒有構(gòu)造函數(shù)可調(diào)用,所以出錯。很明顯在C++中union和class一樣它可以有構(gòu)造函數(shù),不能如此直接引用其成員。struct同樣有這限制。只要我們給其定義一個構(gòu)造函數(shù)什么問題都解決了。示例如下:

class TestUnion
{
enum StoreType{Long,Const_CharP};
union DataUnion //不能匿名
{
DataUnion(const char*); //聲明const char*構(gòu)造函數(shù)
DataUnion(long); //聲明long構(gòu)造函數(shù)
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),stype_(Const_CharP)
{//注意data_(ch),這里直接引用data_
}

TestUnion::TestUnion(long l):data_(l),stype_(Long)
{//注意data_(l),這里直接引用data_
}

TestUnion:ataUnion:ataUnion(const char* ch):ch_(ch)
{
}

TestUnion:ataUnion::DataUnion(long l):l_(l)
{
}

  現(xiàn)在再編譯,如果還不行,你懷疑編譯器有問題是有理由的。好了就寫這么多吧!希望對大家有幫助,我可是花了一個下午的時間呀!如果有什么錯誤,希望來信指出,很希望和大家共同探討C++,共同進(jìn)步(ccplusplus@21cn.com)。
來原:www




歡迎光臨 重慶醫(yī)科大學(xué)論壇 (http://www.espacio12.com/) Powered by Discuz! X3.4