Excerpt from: http://blog.csdn.net/zimingjushi/article/details/6549390
(1) Represents the definition of a bit field within a mechanism (that is, the variable occupies several bit spaces)
typedef struct _xxx{
unsigned char a:4;
unsigned char c;
} ; Xxx
(2) The colon at the back of the constructor functions as a method of assigning a value to a member variable, initializing the list, and more appropriate for the const type of the member variable.
struct _xxx{
_xxx (): Y (0xc0) {}
};
(3) Public: and private: The following colon, which indicates that all the members defined later are publicly or privately held until the next "common:" or "Private:" appears. "Private:" is handled by default.
(4) The class name followed by a colon is used to define the inheritance of the class.
Class derived class Name: Inheritance mode base class name
{
Members of derived classes
};
Inheritance mode: Public, private, and protected, default processing is public.
2. Initialization list of class constructors (Constructor)
First, what is called the constructor (is it verbose?) Everyone in C + + should know, just in case. The so-called constructor is a function with the same name as the class, which differs from the normal function in that it has no return type.
After the constructor is followed by a colon plus an initialization list, each initialization variable is separated by a comma (,). Let's give an example.
Class MyClass
{
Public:
MyClass ();//constructor, no return type, can have parameter list, omitted here
~myclass ();//destructor
int A;
const int B;
}
Myclass::myclass (): A (1), B (1)//initialization list
{
}
The example above shows this usage of the colon, which is explained in several ways:
1) The function of the initialization list is equivalent to assigning the corresponding member variable within the constructor, but there is a difference between the two.
In the initialization list, the variable is initialized, and the assignment is performed within the constructor. The difference between the two is particularly noticeable for operations like const type data. We know that a variable of the const type must be initialized at the time of definition, not a const type variable, so a member variable of the const type can only (and must) be initialized in the initialization list, that is, the following code will be faulted:
Myclass::myclass ()
{
A = 1;//Yes, the effect is equivalent to initializing in the initialization list
b = 1;//error, const variable cannot be assigned operation;
}
2) The order of initialization is the same as the order of the member variable's fame.
First look at the following program:
Myclass::myclass (): B (1), A (b)
{
}
What is the result of such an implementation, A, B? B=1,a=1? No, B=1 and A is a random number. This is very important. In general, when initializing in the initialization list, the order of initialization should be consistent with the order of the declarations, preventing unnecessary errors.
3) for inherited classes, the initialization of the base class is also possible in the initialization list, initialized in the order that the base class is initialized, and then initialized according to the order in which the class's own variables are declared.
The role of colons (:) in C + +