Many important keywords in the C ++ programming language play a very important role in actual programming. The C ++ explicit keyword we introduced today is one of the most frequently used keywords. Let's take a look at this knowledge.
The C ++ explicit keyword is used to modify the class constructor, indicating that the constructor is explicit. Since there is an "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:
[Cpp]
Class MyClass
{
Public:
MyClass (int num)
{
Number = num;
}
Private:
Int number;
};
//.
MyClass obj = 10; // OK, convert int to MyClass
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 a MyClass object, which is actually equivalent to the following operation:
[Cpp]
MyClass temp (10 );
MyClass obj = temp;
MyClass temp (10 );
MyClass obj = temp;
The operations related to all the above C ++ explicit it keywords 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 "explicit", that is, add the explicit it before declaring the constructor, this will prevent this automatic conversion operation. If we modify the constructor of the above MyClass class to be explicit, the following code will not be able to be compiled, as shown below:
[Html]
Class MyClass
{Public:
Class MyClass
{Public: [html] view plaincopyprint? Explicit MyClass (int num );
Explicit MyClass (int num );
} [Html] view plaincopyprint? //.
MyClass obj = 10;
// Err, can't non-explict convert
//.
MyClass obj = 10;
// Err, can't non-explict convert and above are related to the C ++ explicit keyword.
According to the default rules, only one constructor of the parameter also defines an implicit conversion, which converts the data of the corresponding data type of the constructor to this type of object, as shown below:
Class String {
String (const char * p); // use the C-style String p as the initialization value.
//...
}
String s1 = "hello"; // implicit conversion of OK, equivalent to String s1 = String ("hello ");
But sometimes this implicit conversion is not required, as shown below:
Class String {
String (int n); // the intention is to pre-allocate n Bytes to the String
String (const char * p); // use the C-style String p as the initialization value.
//...
}
The following two statements are normal:
String s2 (10); // OK allocates a null String of 10 bytes.
String s3 = String (10); // OK allocates a null String of 10 bytes.
The following two statements are confusing:
String s4 = 10; // compiled and passed. It is also an empty String allocated with 10 bytes.
String s5 = 'a'; // an empty String of int ('A') bytes after compilation
S4 and s5 convert an int type and a char type to an empty string allocated several bytes by implicit conversion, which is easy to misunderstand.
To avoid such errors, we can declare the displayed conversion and use the explicit Keyword:
Class String {
Explicit String (int n); // the intention is to allocate n Bytes to the String in advance
String (const char * p); // use the C-style String p as the initialization value.
//...
}
The addition of explicit will suppress the implicit conversion of String (int n,
The following two statements are still correct:
String s2 (10); // OK allocates a null String of 10 bytes.
String s3 = String (10); // OK allocates a null String of 10 bytes.
The following two statements are not allowed:
String s4 = 10; // implicit conversion is not allowed because the compilation fails.
String s5 = 'a'; // implicit conversion is not allowed because the compilation fails.
Therefore, in some cases, explicit can effectively prevent errors or misunderstandings caused by implicit conversions of constructors.
----------------------------------------------------------
Explicit only applies to constructors and is used to suppress implicit conversions. For example:
Class {
A (int );
};
Int Function (A );
When Function (2) is called, type 2 is implicitly converted to type. This situation is often not the result that programmers want. To avoid it, you can write it like this:
Class {
Explicit A (int );
};
Int Function (A );
In this way, when Function (2) is called, the compiler will give an error message (unless the Function has an overloaded form that uses int as the parameter ), this avoids errors without the programmer's knowledge.
Summary: explicit only applies to constructors and is used to suppress implicit conversions.