The beauty of anonymous unions

Source: Internet
Author: User
Tags anonymous

Within a class, you can actually define some types, such as struct, union, class, but only these three classes can be defined,

Only these three classes can define their respective objects, and then "." Come out or come out, there are only three categories in C + +.

For each of them, there are three ways to define them within a class, such as the Union,
The first way://defines a union type and defines an object belonging to that type UN, so sizeof (X) ==4
Class x{
Public
Union un{
int m_nx;
Char* Pchar;
}un;
};
The second way://Defines a union type, but does not define any objects belonging to that type, so sizeof (X) ==0
Class x{
Public
Union un{
int m_nx;
Char* Pchar;
};
};
The Third Way://Defines an anonymous union type, so it can only be used inside X, and this situation implies

An object belonging to the anonymous union type is defined in X, so sizeof (X) ==4, and this is the most special case, which can be directly

Use M_nx and Pchar through objects of Class X, such as x x;x.m_nx=10;x.pchar= "Hello world!";
Class x{
Public
Union {
int m_nx;
Char* Pchar;
};
};
For the first two methods, a new type with a name is defined inside the class, so you can define the corresponding

To objects of that type, for example: X::un Myun; But this definition requires that the union inside the class be defined with the modifier public,

Otherwise, you cannot define objects that belong to them outside of the class, but only within class X.

Similarly, you can also know that the new type defined by typedef within a class is also true.

It is important to note that the Union uses the third definition of the class to appear, it will have a wonderful effect, very wonderful, perhaps in this

Can be exploited later in the programming process. M_nx and Pchar share a piece of 4 bytes of memory as long as they do not need both M_NX and Pchar

What happens is that you can use only one of them to save memory overhead.

Another use of anonymous federation is automatic type conversion (automatic type conversion is unsafe). For example: When you want to think of a pointer as a decimal number, you can declare it as the following union.

int some_val;

Union

{

void *p;

int n;

};

p = &some_val;

It is not necessary to explicitly convert the pointer to the INT type:

Str::cout << "Address of P is:" << n << Std::endl;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.