The author just system to re-learn the syntax of C + +, see the constructor and initialization table this piece, found that this piece of syntax is a bit complex and very miscellaneous, afraid to forget later, so write this article, later recalled the use.
C + + constructors
3. Constructors (constructor)
Class/struct class name {
constructor Function
class name (formal parameter list) { function body }
};
1) constructor name is the same as class name, no return type
2) The constructor is called automatically when the object is created
3) The constructor is primarily responsible for initializing the object, i.e. initializing the member variable
4) constructors are always called automatically at each object's lifetime, but are only called once
Class A {
A (void) {...}// constructor
};
Int Main (void) {
A; automatically call constructors
}
Constructor overloading (overloading its essence is the name of the compiler's changing function)
Constructors can be overloaded by different parameter tables, selecting matches by constructing the type of the argument when the object is created, representing how different objects are created.
2. Default Constructor (default constructor/ no parameter constructor)
1) If a class does not have any constructors defined, the compiler provides a default parameterless constructor
If a constructor is defined, the compiler will no longer provide a default parameterless constructor, regardless of the parameters.
2) for basic type member variables in a class, do not initialize
3) for member variables of class type, call the corresponding parameterless constructor to initialize the
3. Type conversion constructor (single parameter constructor)
Class Target type {
Target type (source type) {......}
};
You can receive a constructor for a single source type Object argument, which supports implicit conversions from source type to target type
Explicit Keywords: constructors for modifying type conversions, forcing the requirement that such conversions must be displayed
C + + basic syntax constructors and initialization tables