A detailed explanation of anonymous class type and bit domain _c language in C + + programming

Source: Internet
Author: User
Tags anonymous constant

Anonymous class type
A class can be anonymous-that is, you can declare a class without identifier. This is useful when you replace the class name with a typedef name, as follows:

typedef struct
{
  unsigned x;
  unsigned y;
} Point;

Attention
The use of the anonymous class shown in the previous example is useful for preserving compatibility with existing C code. In some C code, it is common to use a typedef in conjunction with an anonymous structure.
An anonymous class is also useful if you want a reference to a class member to appear as if it is not contained in a stand-alone class, as follows:

struct Ptvalue
{point
  ptloc;
  Union
  {
    int ivalue;
    Long lValue;
  };

Ptvalue PTV;

In the above code, you can use the object member selection operator (Ivalue.) to access, as follows:

int i = Ptv.ivalue;

Anonymous classes are bound by certain restrictions. (For more information about anonymous unions, see union.) ) Anonymous class:
Cannot have constructors or destructors.
Cannot be passed as a parameter of a function (unless an ellipsis is used to invalidate the type check).
Cannot be returned as a return value in a function.

C + + bit fields
classes and structs can contain members that occupy less storage space than an integral type. These members are specified as bit fields. The syntax for the bit-field member declaration specification is as follows:
Grammar

Declarator:constant-expression

Note
Optionally, declarator is the name of the member that is accessed in the program. It must be an integral type, including an enumeration type. A constant expression specifies the number of digits occupied by a member in a struct. Anonymous bit field-a bit-domain member without an identifier, which can be used for padding.
Attention
The unnamed bit field with a width of 0 forces the next bit field to be aligned with the next type boundary, where type is the type of the member.
The following example declares the structure that contains a bit field:

Bit_fields1.cpp
//compile with:/ld
struct Date {
  unsigned short nweekday:3;  0..7  (3 bits)
  unsigned short nmonthday:6;  0..31 (6 bits)
  unsigned short nmonth  : 5;  0..12 (5 bits)
  unsigned short nyear   : 8;  0..100 (8 bits)
};

The conceptual memory layout of a date-type object is shown in the following illustration.

Content layout of Data Objects
Note that the length of the nyear is 8 bits and overflows the word boundary of the declaring type unsigned short. Therefore, it begins with the beginning of the new unsigned short. It is not necessary to have all the bit fields fit for the underlying type of objects, and to allocate new storage units based on the number of bits requested in the declaration.
If the declaration of a struct contains an unnamed field with a length of 0 (as shown in the following example),

Bit_fields2.cpp
//compile with:/ld
struct Date {
  unsigned nweekday:3;  0..7  (3 bits)
  unsigned nmonthday:6;  0..31 (6 bits)
  unsigned      : 0;  Force alignment to next boundary.
  Unsigned nmonth  : 5;  0..12 (5 bits)
  unsigned nyear   : 8;  0..100 (8 bits)
};

The memory layout is shown in the following figure.

Layout of data Objects with 0-bit-length fields
The underlying type of a bit field must be an integral type.

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.