Enumeration, union, and enumeration Union

Source: Internet
Author: User

Enumeration, union, and enumeration Union

Enum enumeration and union members share a Variable Buffer

 

Enum is a basic data type, not a constructor type, because it cannot be broken down into any basic data type.

The value of some variables is limited to a limited range.

The enumerated value is a constant, not a variable.

0, 1, 2, 3, 4...

Enum weekday {sun, mon, tue, wed, thu, fri, sat };

Weekday a, B;

Or

Enum weekday {sun, mon, tue, wed, thu, fri, sat} a, B;

Or

Enum {sun, mon, tue, wed, thu, fri, sat} a, B;

 

Only enumeration values can be assigned to enumeration variables, and the element values cannot be directly assigned to enumeration variables. If you want to assign a value to an enumerated variable, you must use forced type conversion.

A = (enum weekday) 2; a = (weekday) 2;

That is, a = tue;

 

Inline week operator ++ (week & rs, int) {// you need to determine whether the value exceeds the range after the overload ++ a ++

Week oldWeek = rs;

Rs = (week) (rs + 1 );

Return oldWeek;

}

 

 

All union members share one space and can only store the values of one of the member variables at a time.

Union foo

{

Int I;

Char c;

Double k;

}

Union foo bar; 8 bytes

 

{Char s [10]; int a ;}10 and 4 occupy 12 bytes

 

Union members cannot be static or referenced. If they are custom types, they cannot have constructors, deconstruct operators, or copy specified operators.

 

Assign values to one member, and the values of other Members do not exist.

Related Article

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.