Use of union in C ++ (2)

Source: Internet
Author: User

Use of union in C ++ (2) Release Date: Source: unknown author: Black and White front-line clicks: 1393 Abstract: 5. How to use it? Assign values to the federated variables only to the members of the variables. The federated variable member is the name of the federated variable. for example, after a is described as a test variable, you can use. class,. office does not allow assignment or other operations only with the name of the federated variable...

5. How to use it?
Assign values to the federated variables only to the members of the variables. The member of the federated variable is:
Joint variable name. member name
For example, after a is described as a test variable, you can use a. Class, A. Office
You are not allowed to assign values only to the federated variable name or perform other operations. You are not allowed to assign values to the federated variables during initialization. Values can only be assigned to programs.
It must be emphasized that only one member value can be assigned to a federated variable at a time. In other words, the value of a federated variable is a member value of the federated variable.

6. Anonymous Union
Anonymous union only notifies the compiler that its member variables share an address. The variables themselves are directly referenced and do not use the common dot operator syntax.

For example:
# I nclude <iostream>
Void main ()
{
Union {
Int test;
Char C;
};
Test = 5;
C = 'a ′;
STD: cout <I <"" <C;
}

As we can see, the Federated component is referenced as a declared common local variable. In fact, for programs, this is exactly how these variables are used. in addition, although defined in a joint declaration, they have the same scope level as any other local variable in the same program. this means that the name of the member in the anonymous union cannot conflict with any other constant identifier in the same scope.
The following restrictions apply to anonymous union:
Because the vertex operator is not used for anonymous union, the elements contained in the anonymous union must be data, and member functions are not allowed or private or protected members are not allowed. In addition, the global anonymous union must be static. Otherwise, it must be placed in an anonymous namespace.

7. Some points to be discussed:
1. What in the Union cannot be stored?
We know that all the items in the Union share the memory, so static and reference cannot be used, because they cannot share the memory.

2. Can classes be combined?

Let's first look at an example:
Class Test
{
Public:
Test (): Data (0 ){}
PRIVATE:
Int data;
};
Typedef union _ Test
{
Test test;
} Ui;
But why?
Because consortium cannot store classes with constructors, analysis functions, and copy operators, the compiler cannot guarantee that these objects will not be destroyed because they share the memory, it cannot be guaranteed that the function can be called when it leaves.
 

3. Why is it anonymous ??

Let's take a look at the next piece of code:
Class Test
{
Public:
Test (const char * P );
Test (INT in );
Const operator char * () const {return
Data. ch ;}
Operator long () const {return data. L ;}
PRIVATE:
Enum type {int, string };
Union
{
Const char * Ch;
Int I;
} Datatype;
Type stype;
Test (test &);
Test & operator = (const Test &);
};
Test: Test (const char * P): Stype
(String), datatype. ch (p ){}
Test: Test (INT in): stype (INT), datatype. L (I ){
}
Do you see any problems? Haha, compilation and translation fail. Why? Is there a problem with datatype. ch (P) and datatype. L (I?
Haha, where is the problem? Let's take a look at what happened when constructing the test object. when creating the test object, we naturally need to call its corresponding constructor. In the constructor, of course, we need to call the constructor of its members, so he wants to call the constructor of the datatype member, but he does not call the constructor.
Error.

Note that this is not an anonymous union! Because it is followed by a piece of data!

4. How to effectively prevent access errors?

Union can save memory space, but there is also a certain risk: Get the value of the current object through an inappropriate data member! For example, the above CH and I requests are staggered.
To prevent such errors, we must define an additional object to track the value types currently stored in the Union. We call this additional object the 'join' discriminant.
A good experience is to provide a set of access functions for all Union data types when processing Union objects as class members.

This article is from ☆★Black and white front lines★☆- Www.hackline.net reprinted please indicate the source, infringement is required!
Link: http://www.hackline.net/a/school/ymbc/C/2009/1221/2341_2.html

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.