1. The explicit keyword in C ++ is used to modify the class constructor, indicating that the constructor is explicit. Since there is "Explicit", there must be "implicit ", so what is explicit and what is implicit?
If the constructor of the C ++ class has a parameter, a default conversion operation will be performed during compilation: Convert the data of the corresponding data type of the constructor to this type of object, as shown below:
Class myclass
{
Public:
Myclass (INT num );
}
....
Myclass OBJ = 10; // OK, convert int to myclass
In the code above, the compiler automatically converts an integer to a myclass object, which is actually equivalent to the following operation:
Myclass temp (10 );
Myclass OBJ = temp;
All the operations above are called "implicit conversions ".
What should we do if we want to avoid this automatic conversion function? Hey, this is the role of the keyword explicit. Declare the class constructor as "display", that is, add the explicit it before declaring the constructor, this will prevent this automatic conversion operation. If we modify the above myclass constructor to display, the following code cannot be compiled, as shown below:
Class myclass
{
Public:
Explicit myclass (INT num );
}
....
Myclass OBJ = 10; // err, can't non-explict convert
2. Explain keywords
When writing an application, the explicit keyword is rarely used. Its function is to "prohibit Single-parameter constructors" and is used for automatic type conversion, A typical example is the container type. In this type of constructor, you can pass the initial length as a parameter to the constructor.
For example:
You can declare such a constructor
| |
Class Array { Public: Explicit array (INT size ); ...... }; |
Here, the explicit keyword plays a vital role. Without this keyword, this constructor can convert int to array. once this happens, you can give the array tribe an integer without causing any problems, such:
In this case, the automatic type conversion of C ++ converts 40 to an array with 40 elements and assigns it to the ARR variable. This result is not the expected result at all. if we declare the constructor as explicit, the assignment operation above will cause the compiler to report an error so that we can detect the error in time. it should be noted that explicit can also prevent "initialization with transformation operations using the value assignment Syntax ";
For example:
Array Arr (40); // correct
Array arr = 40; // Error
Take a look at the following two operations:
X;
Y (x); // explicit type conversion
Another
X;
Y = x; // implicit type conversion
There is a small difference between the two operations. The first method generates a new object of Type Y based on Type X through explicit type conversion; the second method generates a new object of Type Y through implicit conversion.
The application of the explicit keyword is mainly the definition of the constructor mentioned above. You can refer to the application of this keyword to see the STL source code. This keyword is widely used.