Previous: http://www.bkjia.com/kf/201201/116702.html
18.7.1 bit domain
You can declare a special type of data member, called a bit-field, to save a specific number of digits. When a program needs to pass binary data to another program or hardware device, it usually uses a bit field.
The location in the memory is machine-related.
The bit field must be of the overall data type, either signed or unsigned. After a member name is followed by a colon and a constant expression of the specified number of digits, it is pointed out that the member is a single digit.
[Cpp] typedef unsigned int Bit;
Class File {
Bit mode: 2;
Bit modified: 1;
Bit prot_owner: 3;
};
Typedef unsigned int Bit;
Class File {
Bit mode: 2;
Bit modified: 1;
Bit prot_owner: 3;
}; It is usually better to set the bit field to unsigned. The behavior of bit domains stored in the signed type is defined by the implementation.
Bit domain
Access a bit domain in the same way as other data members of the class. For example, a bit field that acts as a private member of a class can only be accessed from the definition of the member function and the friends of the class.
[Cpp] File file = File ();
File. mode = 1;
File. modified = 11;
File file = File ();
File. mode = 1;
File. modified = 11; a class that defines the bit field members usually defines a set of inline member functions to test and set the bit field values.
Generally, the built-in bitwise operator is used to manipulate more than one bitfield.
The address operator (&) cannot be used in bitwise fields. Therefore, it is impossible to reference a pointer to a bitwise domain, and a bitwise domain cannot be a static member of a class.
18.7.2 volatile qualifier
When the object value can be changed by means of compiler control or detection, the object should be declared as volatile. The volatile keyword indicates to the compiler that optimization should not be performed on such objects.
Use the volatile qualifier in the same way as the const qualifier. The volatile qualifier is an additional modifier for the type.
[Cpp] volatile int I;
Volatile TheClass tc100;
Volatile int arr [10];
Volatile int I;
Volatile TheClass tc100;
Volatile int arr [10]; you can declare a volatile pointer to a volatile object and a volatile pointer to a volatile object.
The merging replication control is not applicable to volatile objects.
One important difference between const and volatile is that the composite copy and assign value operators cannot be used to initialize and assign values from volatile objects. The merged copy control members accept the const parameters, which are const references to the class type, but cannot pass the volatile object to a common reference or const reference.
If the class wants to allow the replication of volatile objects, or the class wants to allow the assignment of volatile operations or volatile operations, it must define its own copy constructor and/or the version of the assignment operator.
From xufei96's column