There are many important keywords in C + + programming language that play a very important role in practical programming. The C + + explicit keyword that we introduced to you today is one of the most frequently used keywords. Now let's take a look at this knowledge.
C + + Explicit keyword is used to modify the constructor of the class, indicating that the constructor is explicit, since there is "explicit" then there must be "implicit", then what is the display and what is implicit?
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:
Copy Code code as follows:
Class MyClass
{
Public
MyClass (int num)
{
Number=num;
}
Private
int number;
};
//.
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:
Copy Code code as follows:
MyClass Temp (10);
MyClass obj = temp;
All of the above C + + explicit keyword-related operations 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, the constructor of the class declared as "explicit", that is, in the declaration of the constructor before the explicit can be added to prevent this automatic conversion operations, if we modify the MyClass class above the constructor is explicit, The following code cannot be compiled, as follows:
Copy Code code as follows:
Copy Code code as follows:
explicit MyClass (int num);
}
Copy Code code as follows:
//.
MyClass obj = 10;
Err,can ' t non-explict convert
The above is the C + + explicit keyword related introduction.
By default, the constructor of only one parameter also defines an implicit conversion that converts data of the constructor's corresponding data type to that class object, as shown here:
Copy Code code as follows:
Class String {
String (const char* p); Use the C-style string p as the initialization value
...
}
String S1 = "Hello"; OK implicit conversions, equivalent to string S1 = string ("Hello");
but sometimes this implicit conversion may not be required, as follows:
Copy Code code as follows:
Class String {
String (int n); The intention is to allocate n bytes to the string beforehand
String (const char* p); Use the C-style string p as the initialization value
...
}
The following two kinds of writing are more normal:
Copy Code code as follows:
String S2 (10); OK allocates an empty string of 10 bytes
String s3 = String (10); OK allocates an empty string of 10 bytes
The following two kinds of writing are more puzzled:
Copy Code code as follows:
String S4 = 10; Compile pass, also allocate 10 bytes of empty string
String s5 = ' a '; An empty string that allocates int (' a ') bytes by compiling
S4 and S5 are easily misunderstood by converting an int and char, implicitly to an empty string that allocates several bytes.
To avoid this error, we can declare the conversion of the display, using the EXPLICIT keyword:
Copy Code code as follows:
Class String {
explicit String (int n); The intention is to allocate n bytes to the string beforehand
String (const char* p); Use the C-style string p as the initialization value
...
}
By adding explicit, the implicit conversion of string (int n) is suppressed.
The following two types of writing are still correct:
Copy Code code as follows:
String S2 (10); OK allocates an empty string of 10 bytes
String s3 = String (10); OK allocates an empty string of 10 bytes
The following two types of writing are not allowed:
Copy Code code as follows:
String S4 = 10; Compilation does not pass, implicit conversions are not allowed
String s5 = ' a '; Compilation does not pass, implicit conversions are not allowed
Therefore, at some point, explicit can effectively prevent errors or misunderstandings caused by implicit conversions of constructors
----------------------------------------------------------
Explicit only works on constructors to suppress implicit conversions. Such as:
Copy Code code as follows:
Class A {
A (int a);
};
int Function (a);
When a Function (2) is invoked, 2 is implicitly converted to type A. This is often not the result that programmers want, so to avoid it, you can write:
Copy Code code as follows:
Class A {
Explicit a (int a);
};
int Function (a);
Thus, when the function (2) is invoked, the compiler gives an error message (unless the function has an overloaded form with an int), which avoids the error if the programmer is unaware.
Summary: Explicit only works on constructors to suppress implicit conversions.