#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*构造函数 DataUnion(long); //声明long构造函数 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://dhu.myujob.com/) | Powered by Discuz! X3.3 |