First, the explicit keyword in C ++ can only be used to modify the class constructor with only one parameter. Its function is to indicate that the constructor is displayed, rather than implicit, another keyword corresponding to it is implicit,
It indicates hidden, and the class constructor is declared as implicit (implicit) by default ).
So what is the difference between the declared constructor and the implicit declaration? Let's take a look at the following example:
Class cxstring // class declaration without the explicit keyword, that is, the default value is the implicit declaration {public: char * _ pstr; int _ size; cxstring (INT size) {_ size = size; // default size of string _ pstr = malloc (size + 1); // allocate string memory for memset (_ pstr, 0, size + 1 );} cxstring (const char * P) {int size = strlen (p); _ pstr = malloc (size + 1); // allocate string memory strcpy (_ pstr, P ); // copy string _ size = strlen (_ pstr);} // The Destructor is not discussed here, and omitted ...}; // The following is the call: cxstring string1 (24); // This is OK, and the 24-byte memory cxstring string2 = 10 is pre-allocated for cxstring; // This is OK, pre-allocate a 10-byte memory cxstring string3 for the cxstring; // This is not acceptable because there is no default constructor and the error is: "cxstring ": there is no suitable default constructor available cxstring string4 ("aaaa"); // This is OK cxstring string5 = "BBB"; // This is also OK, the call is cxstring (const char * P) cxstring string6 = 'C'; // This is also OK. Actually, the call is cxstring (INT size ), and the size is equal to the 'C' ASCII code string1 = 2; // This is also OK, pre-allocate 2 bytes of memory string2 = 3 for cxstring; // This is also OK, pre-allocate a 3-byte memory for the cxstring string3 = string1; // This is also OK, at least the compilation is OK, however, if the free release _ pstr memory pointer is used in the destructor, an error may occur. The complete code must overload the operator "=" and process the memory release.
In the above Code, "cxstring string2 = 10;" Why is this statement possible? In C ++, if the constructor has only one 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. that is to say, "cxstring
String2 = 10; "in this Code, the compiler automatically converts an integer to a cxstring object, which is actually equivalent to the following operation:
Cxstring string2 (10); or cxstring temp (10); cxstring string2 = temp;
However, the _ size in the above Code indicates the size of the string memory allocation, so the second sentence of the call is "cxstring string2 = 10;" and the sixth sentence "cxstring string6 = 'C '; "This is a nondescribable and confusing situation. is there any way to stop this usage? The answer is to use the explicit keyword.
Modify the above Code as follows:
Class cxstring // class declaration using the keyword "Explicit", showing the conversion of {public: char * _ pstr; int _ size; explicit cxstring (INT size) {_ size = size; // The code is the same as above, omitted ...} cxstring (const char * P) {// The code is the same as above, omitted ...}}; // The following is the call: cxstring string1 (24); // This is OK's cxstring string2 = 10; // This is not acceptable, because the explicit it keyword cancels the implicit conversion of cxstring string3; // This is not acceptable, because no default constructor cxstring string4 ("aaaa"); // This is the OK cxstring string5 = "BBB"; // This is also OK, the call is cxstring (const char * P) cxstring string6 = 'C'; // This is not acceptable. Actually, the call is cxstring (INT size ), and the size is equal to the 'C' ASCII code, but the explicit keyword cancels the implicit conversion of string1 = 2; // This is also not acceptable, because the implicit conversion of string2 = 3 is canceled; // This is also not acceptable, because the implicit conversion string3 = string1 is canceled; // This is also not acceptable, because the implicit conversion is canceled unless the class implementation operator "=" is overloaded.
The explicit keyword is used to prevent implicit automatic conversion of class constructor.
As mentioned above, the explicit keyword is only valid for class constructors with one parameter. If the class constructor parameter is greater than or equal to two, no implicit conversion is generated, so the explicit keyword is invalid. for example:
Class cxstring // The explicit keyword is invalid when the class constructor parameter is greater than or equal to {public: char * _ pstr; int _ age; int _ size; explicit cxstring (INT age, int size) {_ age = age; _ size = size; // The code is the same as above, omitting ...} cxstring (const char * P) {// The code is the same as above, omitted ...}}; // whether the explicit keyword is the same at this time
However, when all parameters except the first parameter have default values,
The explicit keyword is still valid. In this case, only one parameter is input when the constructor is called, which is equivalent to a class constructor with only one parameter. The example is as follows:
Class cxstring // use the keyword explicit to declare {public: int _ age; int _ size; explicit cxstring (INT age, int size = 0) {_ age = age; _ size = size; // The code is the same as above, omitted ...} cxstring (const char * P) {// The code is the same as above, omitted ...}}; // The following is the call: cxstring string1 (24); // This is OK's cxstring string2 = 10; // This is not acceptable, because the explicit it keyword cancels the implicit conversion of cxstring string3; // This is not acceptable, because no default constring1 = 2; // This is also not acceptable, because the implicit conversion of string2 = 3 is canceled; // This is also not acceptable, this is because the implicit conversion string3 = string1 is canceled; // This is not acceptable because the implicit conversion is canceled unless the class implementation operator "=" is overloaded.
The above is a detailed description of the C ++ explicit keyword.