C++ explicit keyword details

Keywords: ascii

First, the explicit keyword in C++ can only be used to modify a class constructor with only one parameter. Its function is to show that the constructor is displayed, not implicit. The other key corresponding to it is implicit, meaning hidden. Class constructors are declared implicit by default.

So what's the difference between a constructor that displays a declaration and an implicit declaration? Let's look at the following example:

  1. class CxString  //Class declarations that do not use the explicit keyword are implicit declarations by default  
  2. {  
  3. public:  
  4.     char *_pstr;  
  5.     int _size;  
  6.     CxString(int size)  
  7.     {  
  8.         _size = size;                //Preset size of string  
  9.         _pstr = malloc(size + 1);    //Allocate string memory  
  10.         memset(_pstr, 0, size + 1);  
  11.     }  
  12.     CxString(const char *p)  
  13.     {  
  14.         int size = strlen(p);  
  15.         _pstr = malloc(size + 1);    //Allocate string memory  
  16.         strcpy(_pstr, p);            //Copy strings  
  17.         _size = strlen(_pstr);  
  18.     }  
  19.     //Destructors are not discussed here.  
  20. };  
  21.   
  22.     //The following is the call:  
  23.   
  24.     CxString string1(24);     //This is OK, which pre-allocates 24 bytes of memory for CxString  
  25.     CxString string2 = 10;    //This is OK, which pre-allocates 10 bytes of memory for CxString  
  26.     CxString string3;         //This is not possible because there is no default constructor and the error is "CxString": no appropriate default constructor is available  
  27.     CxString string4("aaaa"); //This is OK's  
  28.     CxString string5 = "bbb"//This is also OK, calling CxString(const char * p)  
  29.     CxString string6 = 'c';   //This is OK. In fact, it calls CxString(int size) and size equals ascii code of'c'.  
  30.     string1 = 2;              //This is also OK, which pre-allocates 2 bytes of memory for CxString  
  31.     string2 = 3;              //This is also OK, which pre-allocates 3 bytes of memory for CxString  
  32.     string3 = string1;        //This is OK, at least compilation is fine, but if you use free release_pstr memory pointer in the destructor, you may get an error. The complete code must overload the operator "=" and handle memory release in it.  

In the above code, "CxString string 2 = 10;" Why is it possible? In C++, if the constructor has only one parameter, then there will be a default conversion operation at compile time: the data corresponding to the data type of the constructor will be converted to this kind of object. That is to say, "CxString string 2 = 10;" This code, the compiler automatically converts the integer to CxS. The tring class object is actually equivalent to the following operations:

  1. CxString string2(10);  
  2. or  
  3. CxString temp(10);  
  4. CxString string2 = temp;  

However, the _size in the above code represents the size of the string memory allocation, so the second sentence of the call "CxString string 2 = 10;" and the sixth sentence "CxString string 6 = c';" seem incoherent and confusing. What can prevent this use? The answer is to use the explicit keyword. Let's modify the above code as follows:

  1. class CxString  //Use the keyword explicit class declaration to display the transformation  
  2. {  
  3. public:  
  4.     char *_pstr;  
  5.     int _size;  
  6.     explicit CxString(int size)  
  7.     {  
  8.         _size = size;  
  9.         //Ibid., omit...  
  10.     }  
  11.     CxString(const char *p)  
  12.     {  
  13.         //Ibid., omit...  
  14.     }  
  15. };  
  16.   
  17.     //The following is the call:  
  18.   
  19.     CxString string1(24);     //This is OK's  
  20.     CxString string2 = 10;    //This is not possible because the explicit keyword cancels the implicit conversion  
  21.     CxString string3;         //This is not possible because there is no default constructor  
  22.     CxString string4("aaaa"); //This is OK's  
  23.     CxString string5 = "bbb"//This is also OK, calling CxString(const char * p)  
  24.     CxString string6 = 'c';   //This is not possible. In fact, CxString(int size) is called, and size equals ascii code of'c', but the explicit keyword cancels the implicit conversion.  
  25.     string1 = 2;              //This is also not possible because implicit conversions have been abolished  
  26.     string2 = 3;              //This is also not possible because implicit conversions have been abolished  
  27.     string3 = string1;        //This is also not possible because implicit conversions are cancelled unless the class implements the overload of the operator "="  

The function of the explicit keyword is to prevent the implicit automatic conversion of class constructors.

As mentioned above, the explicit keyword is only valid for a class constructor with one parameter. If the class constructor parameter is greater than or equal to two, there will be no implicit conversion, so the explicit keyword is invalid. For example:

  1. class CxString  //explicit keywords are invalid when class constructor parameters are greater than or equal to two  
  2. {  
  3. public:  
  4.     char *_pstr;  
  5.     int _age;  
  6.     int _size;  
  7.     explicit CxString(int age, int size)  
  8.     {  
  9.         _age = age;  
  10.         _size = size;  
  11.         //Ibid., omit...  
  12.     }  
  13.     CxString(const char *p)  
  14.     {  
  15.         //Ibid., omit...  
  16.     }  
  17. };  
  18.   
  19.     //Whether there is an explicit keyword or not is the same at this time.  

However, there is an exception, that is, the explicit keyword is still valid when all parameters except the first parameter have default values. At this time, when the constructor is called, only one parameter is passed in, which is equivalent to a class constructor with only one parameter. The example is as follows:

  1. class CxString  //Use the keyword explicit declaration  
  2. {  
  3. public:  
  4.     int _age;  
  5.     int _size;  
  6.     explicit CxString(int age, int size = 0)  
  7.     {  
  8.         _age = age;  
  9.         _size = size;  
  10.         //Ibid., omit...  
  11.     }  
  12.     CxString(const char *p)  
  13.     {  
  14.         //Ibid., omit...  
  15.     }  
  16. };  
  17.   
  18.     //The following is the call:  
  19.   
  20.     CxString string1(24);     //This is OK's  
  21.     CxString string2 = 10;    //This is not possible because the explicit keyword cancels the implicit conversion  
  22.     CxString string3;         //This is not possible because there is no default constructor  
  23.     string1 = 2;              //This is also not possible because implicit conversions have been abolished  
  24.     string2 = 3;              //This is also not possible because implicit conversions have been abolished  
  25.     string3 = string1;        //This is also not possible because implicit conversions are cancelled unless the class implements the overload of the operator "="  

The above is a detailed description of the C++ explicit keyword.

Posted by Adam W on Mon, 25 Mar 2019 20:54:29 -0700