(David's reading note) Some think about using Union in C ++

xiaoxiao2021-03-06  75

David

:

During this time, the old information is

See some articles

Although it is a small problem

Not very likely to use

But it is also a knowledge point

I specialize in sharing with everyone.

Some understandings of the author of the article related to this are wrong

, I wrote this article

It is also some wrong understanding to correct for the author.

.of course

If my understanding is wrong

, I also invite everyone to criticize

.

C

Although it is B

.

A new language

, But it has a thousand connection with C.

Although B

.

Repeatedly insisted

But I am still willing to put C

is considered to be C

.

We should use the Convention in C.

union

This is what I want to give this article.

Although C

makes we can expand some new things in

,but

I suggest you don't do it.

After reading this article

I think you are probably thinking.

.

C because there is no concept

All types can act as a combination of basic types

,Thus, in

Union contains

Struct is also a natural thing.

, To c

After

Since it is generally considered C

Struct

Class basic equivalent

So, then

Is there a class member in Union?

Let's take a look at the following code.

:

Struct Testunion

{

Testunion

() {}};

Typedef Union

{

Testunion Obj

UT

;

int

main

(

Void

) {

Return

0

}

Compile this procedure

We will be told

:

Error C2620

:

union

'__unnamed'

: MEMBER

'Obj' HAS User

-defined constructor

OR NON

-Trivial

Default Constructionor

And if you remove the constructor, there is no dry constructor

Other OK

.

Why does the compiler do not allow our

UNION member has a constructor?

I can't find the interpretation of the comparative authority about this issue.

, For this issue

My explanation is

:

If c

standard allows us

Union has a constructor

So, then

Do you want this constructor when you are allocated?

? If the answer is YES

, Then some memory allocation operations are included in the Testunion constructor

Or other modifications to the entire Application status

So, then

If I want to use Obj in the future

Things may be relatively reasonable

But if I don't use the OBJ member at all.

• Because OBJ's introduction caused by system status is obviously unreasonable

;on the contrary

If the answer is NO

So once we will choose OBJ in the future

, All information is not initialized

(If it is ordinary

Struct

,no problem

,but

If there is a virtual function?

?). Further

, Suppose now our

Union is not only one testunion Obj

And a testunion2 Obj2

, Both have constructor

And all execute some memory allocation in the constructor

(Even many other things

), Then

If the OBJ is constructed first

After construction OBJ2

, The result of execution is almost certainly caused by the leak of memory.

.

In view of the above troubles

(May have more trouble

)

Union

The compiler is only responsible for allocation space

Not responsible for performing additional initialization work

, In order to simplify work

As long as we provide a constructor

, Will receive the above Error

.

Concrete

, In addition to impovering functions

Destructor

/ Copy constructor

/ Assignment operator is not available

.

In addition

If we contain anything in our class

Virtual function

, Compile

We will receive the following error message

:

Error C2621

:

union

'__unnamed'

: MEMBER

'obj' HAS COPY CONSTRUCTOR

and so

Dispel

Contains constructor in Union

/ Destructor / copy constructor

/ Assignment operator

/ The idea of ​​the class member variable of virtual functions

, Old man practical your C style

Struct

!

but

Defining a normal member function is OK

Because this will not make

Class with C style

Struct has any essential difference

, You can completely

Class is understood as a C style

Struct

N all-course functions

.

right now

, Then look at the interior in the class

What is different when union?

Take a look at the procedure below

, Please pay attention to reading program prompts

:

Class testunion

{

Union Dataunion

{

Dataunion

(

Const

charr

*);

Dataunion

(

Long

);

Const

charr

* Ch_

;

Long L_

} DATA_

;

public

:

Testunion

(

Const

charr

* Ch

);

Testunion

(

Long L

};

Testunion

:: Testunion

(

Const

charr

* Ch

: DATA_

(CH

)

// if you want to use initialzing list to initiate a nested-union member, The Union Must Not Be Anonymous and Must Have a Construction.

{}

Testunion

:: Testunion

(

Long L

: DATA_

(l

) {}

Testunion

:: Dataunion

:: Dataunion

(

Const

charr

* Ch

: Ch_

(CH

) {}

Testunion

:: Dataunion

:: Dataunion

(

Long L

: L_

(l

) {}

int

main

(

Void

) {

Return

0

}

As shown above

, C

Union can also include constructor

,but

Although this is supported by the language

But it is really a poor programming habit

,therefore

I don't intend to excessive instructions on the above program.

I recommend the following programming style

:

Class testunion

{

Union Dataunion

{

Const

charr

* Ch_

;

Long L_

} DATA_

;

public

:

Testunion

(

Const

charr

* Ch

);

Testunion

(

Long L

};

Testunion

:: Testunion

(

Const

charr

* Ch

) {

Data_

.ch_

= CH

}

Testunion

:: Testunion

(

Long L

) {

Data_

.l_

= L

}

int

main

(

Void

) {

Return

0

}

It is completely C style

.

and so

Accept this conclusion

:

Please follow the CONVENTION in C.

union

Don't try to use any C as much as possible

additional characteristics

.

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

New Post(0)