Application Analysis of Union in C
Author: Yuanxiao Kai
South China University of Technology Computer Research Institute North District R & D
Email - ccplusplus@21cn.com
Foreword
Programmers who are familiar with C know the usage of UNION (combined), using Union to store different types of data types with the same storage space, saving memory space. Use "." And "->" to be accessed directly when accessing its members. After C appears, it inherits Union and retains its characteristics in C. However, Union in C has a new extension, this requires everyone to understand, or you will feel puzzled and confused. Let me talk about two points.
First, store the object in Union
In the Union in C, any type of built-in data type can be stored, then the Union can store the object in C ? Let us look at an example, this can explain the problem than any speech, isn't it?
#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;
}
Oh! Too unfortunate, this is not, the object of the Testunion class cannot be stored in Union, but Union can store Struct in C, why can't you store the object of the class? Very simple, please, Union in C can store Struct with constructor? Yes, Structs in C are not constructed. So if UNION can store the object of the class with the constructor in C , it is not very logical, isn't it compatible with C and C? Nice, just because of this, Union cannot store objects with a class of constructors, but can store objects that do not construct a class, which is consistent with C, do not want to trust you try. The statement of the Testunion class is modified as follows:
Class testunion
{
PUBLIC:
INT DATA_;
}
Compile, everything is OK! . But this lost the initialization characteristics of C , so there is no meaning. So, do you think that I am talking about nonsense (I think so ^ _ ^). I am just talking about it in C , I don't recommend everyone to use (absolutely not recommended). But we can store pointers for objects in UNITION to reference different object types. Don't tell you more, let's try it!
Second, the initialization of Union in the class
Due to the shared memory characteristics of UNION, we can store our class to different types without waste memory space, we can declare a Union storage of different types of pointers, examples:
#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
Cout << lobj << Endl;
Return 0;
}
Hey! It's really unfortunate, compiled, it seems that there is no problem, why? Does Data_.ch_ (ch) and DATA_.L_ (L) have problems? If you ask a C programmer, he will tell you that there is absolutely no problem. You won't doubt your compiler if you have any questions! Sorry! I thought about it at first, it is really embarrassed. Request, confused. Let's take a look at what happened to construct the testunion object, so you will understand. When you create a Testunion object, you should naturally call its corresponding constructor, of course, in the constructor, it is necessary to call the constructor's constructor, so it is going to call the constructor of the Union member, but it is anonymous, there is no constructive function Call, so an error. It is clear that it can have a constructor in C , which can be constructed and cannot be directly referenced. Struct also has this limit. As long as we define a constructor, what is the problem. Examples are as follows:
Class testunion
{
ENUM StoreType {long, const_charp};
Union dataUnion // cannot be anonymous
{
Dataunion (const char *); // Declaration const char * constructor
Dataunion (long); // Declare a LONG constructor
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)
{// Note DATA_ (CH), here directly collect DATA_
}
Testunion :: Testunion (long L): DATA_ (L), Stype_ (long) {// Note Data_ (L), here is directly referenced DATA_
}
Testunion :: DataUnion :: Dataunion (const char * ch): CH_ (CH)
{
}
Testunion :: DataUnion :: Dataunion (long L): l_ (l)
{
}
Now compile, if you can't still do, you suspect that there is a reason for the compiler. Well to write so much! I hope to help everyone, I spent an afternoon! If there is anything wrong, I hope that I will tell it that I hope to discuss C and make progress together (ccplusplus@21cn.com).
Acknowledgments!