Further understanding of Union and some deep application

xiaoxiao2021-03-06  68

Although it is usually used in the development of procedures

union

Although when I first learned C language

,

Union chapter is slightly introduced by the teacher

,although

Self-think of itself

Union's understanding is enough

,but

, Finishing the previous article

<(David's reading notes) C thinking in the use of Union>

Online discussions drive me some energy to pay attention to this basic language characteristics.

And write this article

.

The following is in MSDN

Overview of UNION

It seems to be a bit boring

,but

Sometimes I can give us a lot of tips.

, When we pay attention to the same thing from the point of application

Many deeper considerations have been ignored by us.

.and so

, Read on

Some things inside may be you ignored.

.

Unionunion

[tag "

] {MEMBER

-List

} [Declarators

];

union

] Tag Declarators

;

THE

Union Keyword Declares A

Union Type

and

/

OR a variable of a

Union Type

.

A

Union IS A User

-Defined Data Type That Can Hold Values ​​Of Different Types At Different Times

IT IS Similar TO A STRUCTURE

Except That All of Its Members Start At The Same Location in Memory

. A

Union Variable Can Contain Only ONE OF ITS MEMBERS AT

a time

Size of the

Union is at Least The size of the largest member

(David)

: I can't think of the situation greater than

).

For related information

SEE

Class

,

Struct

,

And anonymous union

.

DECLARING A Union

Begin The Declaration of A

Union with the

Union Keyword

,

And Enclose The Member List in Curly Braces

:

Union unknown

// Declare Union Type

{

Char CH

;

INT i

;

Long L

;

Float F

;

Double D

} Var1

;

// Optional Declaration of Union Variableusing a union

A c

Union is a limited form of there

Class Type

IT CAN Contain Access Specifiers

(

public

,

protected

,

Private

), Member Data

,

And Member Functions

, Including Constructionors

And Destructors

It cannot Contain

Virtual functions

OR Static Data Members

IT

Cannot Be Used As a Base

Class

NOR CAN IT HAVE BASES CLASSES

DEFAULT Access of MEMBERS IN A

Union IS

public

.

A c

Union Type Can Contain Only Data MEMBERS

.

IN C

YOU MUST USE THE

Union Keyword to Declare Aunion Variable

In c

, the

Union Keyword IS Unnecessary

:

EXAMPLE

1

Union unknown var2

;

// c Declaration of a union variableunknown var3

;

// C Declaration of a Union VariableExample

2

A variable of a

Union Type Can Hold One Value of Any Type Declared in

union

Use the member

-Serection

Operator

(.) TO Access a member of a

union

:

Var1

.i

=

6

;

// use variable as integervar2

.

=

5.327

;

// Use variable as double

In order to avoid a slight distortion of the above text

I deliberately rendered it.

, But here, some summary

:

1.

Union is a special

Struct

/

Class

It is a type that can be used to accommodate multiple types.

But with

Struct

/

Class is different

All member variables share the same storage space

(The biggest size of the member type

), Which makes it have a variety of features

You can switch anything in different members

No need to convert with mandatory types

But this also makes you modified it as a member variable without affecting another member variable.

;

2.

Union can also be constructed

/ Destructor

You can also include access identifiers

, But cannot include virtual functions or static member variables

/method

.

About use

Some problems need to be paying attention to when union

, Can refer to my previous article

: <(David reading notes)

) C

use

Some Thoughts on Union

".

Let's talk about some interesting and meaningful

UNION application

.

1.in_addr

Struct in_addr

{

union

{

Struct

{U_char s_b1

S_B2

S_B3

S_B4

} S_UN_B

;

Struct

{U_SHORT S_W1

, S_W2

} S_UN_W

;

u_long s_addr

} S_un

};

Above

Struct

, People who wrote Socket application

, Definitely used it

I don't know if you pay attention

It contains a very interesting

union

The

Union's members have the same size

Different expressions representing the same information, respectively

You can also use this feature to provide the same information as the same information.

But pay attention to

, When conducting a cross-platform application

The influence of byte order may cause some unnecessary troubles.

.

2. Anonymous

union

anonymous

Union is no name and declaration list

union

, Which with

'__unnamed'

Union is not a matter

Its declaration is as follows

:

union

{MEMBER

-List

}

anonymous

Union only notifies the compiler It share an address to share

And the variable itself is directly referenced.

Do not use the usual point operator syntax

It is also

,anonymous

UNION has the same scope level with other variables in the same block

Need to pay attention to naming conflicts

.

Please see the example below

:

#include

Struct DataForm

{

ENUM DATATYPE

{Chardata

=

1

INTDATA

StringData

}

DataType Type

;

// Declare an anonymous union.union

{

Char chcharmem

;

charr

* szstrmem

;

INT Iintmem

};

Void Print

();

Void DataForm

:: Print

() {

// based on the Type of the data, print the// appropriate data type.

Switch

(Type

) {

Case Chardata

:

cout

<< chcharmem

;

Break

;

Case INTDATA

:

cout

<< SzstrMem

;

Break

;

Case stringdata

:

cout

<< Iintmem

;

Break

}}

In addition

,anonymous

Union also has the following constraints

:

1

). Because anonymity is unit, no point operator

Therefore, the element contained in anonymously in anonymously must be data

Do not allow member functions

, You can't include private or protected members

;

2

) Global anonymity must be static

(

Static

), Otherwise you must put it in anonymous namespace.

.

Note note

:

Anonymous

Union concept

You may have some strangers

, But developers for Windows applications

There is a structure that is often used, contains anonymity.

union

, It is Variant

Maybe you don't pay attention to it.

:

TypeDef struct farstruct tagvariant variant

;

Typedef struct farstruct tagvariant variantarg

;

Typedef struct tagvariant

{

Vartype VT

;

Unsigned short wreserved1

;

Unsigned short wreserved2

;

Unsigned short wreserved3

;

union

{

BYTE BVAL

;

// vt_ui1. Short IVAL

;

// vt_i2.

Long Lval

;

// vt_i4.

Float fltval

;

// vt_r4. // ...

};

3. Utilization

Union Type Conversion

I have already said before

,

Union has a variable characteristic

You can switch anything in different members

No need to convert with mandatory types

Here, this is an example of this

(in fact

1 has been well explained this

):

#include

Using Namespace STD

;

Struct Data

{

Char C1

;

Char C2

};

int

main

() {

union

{

INT i

;

Data Data

}_Ut

;

_ut

.i

=

0x6162

;

cout

<<

"_UT.DATA.C1 ="

<< _ut

.DATA

.C1

<< ENDL

<<

"_UT.DATA.C2 ="

<< _ut

.DATA

.c2

<< ENDL

;

Return

0

}

Need to remind you

, Data type conversion

Not

Union's expertise

It is just a sustained characteristic.

.because

,use

Union is used to translate very vulnerable to platform

, As the program is used in Intel X86

Windows

2000

The output is output when VC6

:

_ut

.DATA

.C1

= B

_ut

.DATA

.c2

= A

(Note

: Because the architecture of Intel CPU is Little Endian

)

And on the SUN SPARC

The result you get is

:

_ut

.DATA

.C1

=

_ut

.DATA

.c2

= (Note

: Because Big Endian is used

The first two bytes are

0x0000

)

Even on the same platform

Do not use when converting between Integer types and REAL types

union

,otherwise

, You will get you a conclusion that you are inexplicably

(This is due to the process of processing of the REAL type

This mode has great differences on each platform

,at the same time

According to C

Standard

This will cause this practice

"undefined behavior"

).

Type conversion for using reference

, Refer to

.

转载请注明原文地址:https://www.9cbs.com/read-87580.html

New Post(0)