ArticleDirectory
- Const member functions
- Name Search
- Constructor initialization list
- Default constructor
- Implicit type conversion
Const member functions
A member function can be reloaded based on whether the member function is const. Similarly, a function can be reloaded based on whether a pointer-type parameter points to const. A const object can only use const members. A non-const object can use any member, but a non-const version is a better match.
Each member function has an implicit this pointer. For non-const member functions, the pointer type isT const *For const member functions, the pointer type isConst t const *.
Name Search
Name Search (the process of searching for declarations that match the given name)
- First, find the name declaration in the block using the name. Only the name declared before this option is used.
- If the name cannot be found, search for it in the surrounding scope.
- If no declaration is foundProgramError.
The class definition is actually processed in two phases:
- First, compile the member declaration;
- The definition itself is compiled only when all members appear.
Find the name in the class member declaration:
- Check the class member declaration that appears before the name is used;
- If the first step cannot be found, check the declarations in the scope that contains the class definition and the declarations that appear before the class definition.
Note: you must first define the type names in the class to use them as the data member type, or the return type or parameter type of the member function. The compiler processes the statements in the order they appear in the class. Generally, the name must be defined before use. In addition, once a name is used as a type name, it cannot be defined repeatedly.
Name Search in the class member definition:
- First, check the declaration in the partial scope of the member function;
- If the declaration of this name cannot be found in the member function, check the declaration of all class members;
- If the declaration of this name is not found in the class, check the declaration in the scope before the member function definition.
Constructor initialization list
In terms of concept, the constructor can be considered to be executed in two phases:
- Initialization phase: Initialize all data members. If the initialization type is provided in the initialization list, it is initialized as required. If not, the default constructor is executed for the class type, and the built-in and composite types are not initialized.
- General computing stage: It is composed of all statements in the constructor.
Some members must be initialized in the constructor initialization list. For such members, assigning values to them in the constructor body does not work.No default constructor class typeAndConstOrReference TypeIn the constructor initialization list.
Assigning values to non-class data members or using initialization are equivalent in terms of results and performance.
The constructor initialization list only specifies the values used to initialize members, and does not specify the initialization execution order. The order in which members are initialized is the order in which they are defined.
Default constructor
The compiler automatically generates a default constructor only when no constructor is defined for a class. The default constructor for merging uses the same rule as the variable initialization to initialize members.
In some cases, the default constructor is implicitly applied by the compiler. If the class does not have a default constructor, the class cannot be used in these environments. The following assumes that the nodefault class does not have a default constructor, which means:
- Each constructor of each class with nodefault members must be explicitly initialized using the initialization list;
- Cannot be used as the element type for dynamically allocating arrays;
- This type of static allocation array must provide an Explicit initialization type for each element;
- If there is a container that saves this type of object, such as vector, it cannot use a constructor that accepts the container size without providing an element initialization function at the same time.
Implicit type conversion
Constructors with a single parameter define an implicit conversion from the parameter type to this type. When the constructor is declared as explicit, the compiler will not use it as the conversion operator. Note that the explicit keyword can only be used for the constructor declaration within the class.
Generally, unless you want to define implicit conversions for obvious reasons, the single-parameter constructor should explain. Setting the constructor to explicit can avoid errors. When the conversion is available, you can display the constructor objects.
The constructor is called directly, for example, T (PARAM) to create a temporary object. It can also be understood as forced type conversion.
Any constructor can use an explicit call to create a temporary object.