C + + Explicit keyword is mainly used to modify the class's constructor, indicating that the constructor is explicit, prohibiting the implicit conversion of single parameter constructors.
If the C + + class constructor has a parameter, then there is a default conversion operation at compile time: Converts the data of the constructor's corresponding data type to that class object, as shown here:
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 the MyClass class object, which is actually equivalent to the following operation:
MyClass Temp (10);
MyClass obj = temp;
All of the above actions are called "implicit conversions".
If you want to avoid this automatic conversion function, you can prevent this automatic conversion operation by adding the EXPLICT keyword before declaring the constructor and declaring the class's constructor as "display".
To modify the constructor of the above MyClass class for display, the following code will not be able to compile through, as follows:
Class MyClass
{
Public
explicit MyClass (int num);
}
MyClass obj = 10; Err,can ' t non-explict convert