Use of the Union (Union) in C + +

Source: Internet
Author: User

1 typedef Union para2 {3   Doubletmp[Ten];4   struct5 {6    DoubleA;7  Doubleb;8    DoubleC;9   DoubleD;Ten }NP; One }npara; A //or as shown below - Union para - { the   Doubletmp[Ten]; -   struct - { -    DoubleA; +  Doubleb; -    DoubleC; +   DoubleD; A }NP; at }; - typedef Union para Npara; -  - //See the use of typedef in detail, this article mainly explains the use of Union, not to repeat


Use of Union unions:

For example, after using a TypeDef, we can declare a union object like this

Npara Nlpara; Call can be used when the NLPARA.NP.A;

Or declare a union pointer,

Npara *pointer;//But only a pointer is declared, the pointer is not initialized, there is no point to the determined object, and the debug code has a pointer error.

So to initialize the pointer, you can do this in C + +:

Pointer=new npara;//Even if the new Union object is assigned to the current pointer,

We know that new and delete are generally interdependent, and at the end of the program, you can use

if (Null!=pointer)

{

Delete pointer;

Pointer=null;

}

In addition, if you create a new pointer object in the constructor, delete it from the destructor.

Use of the Union (Union) in C + +

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.