| #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; } |
| class TestUnion { public: int data_; }; |
| #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; } |
| 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){ } |
| 歡迎光臨 電子科大論壇-非清水河畔 (http://www.hallmarkedu.com/) | Powered by Discuz! X3.4 |