Translated from: http://www.cnblogs.com/ymy124/p/3632634.html
First, the explicit keyword in C + + can only be used to decorate a class constructor that has only one argument, which is to show that the constructor is displayed, not implicitly, and that another keyword corresponding to it is implicit, meaning hidden, The class constructor is declared by default as implicit (implicit).
So what's the difference between a declared constructor and an implicit declaration? Let's take a look at the following example:
- Class Cxstring //is not declared with the explicit keyword, which is implicitly declared by default
- {
- Public
- Char *_pstr;
- int _size;
- cxstring (int size)
- {
- _size = size; //The preset size of string
- _PSTR = malloc (size + 1); //Allocate a string of memory
- memset (_pstr, 0, size + 1);
- }
- Cxstring (const char *p)
- {
- int size = strlen (p);
- _PSTR = malloc (size + 1); //Allocate a string of memory
- strcpy (_PSTR, p); //Copy string
- _size = strlen (_PSTR);
- }
- //destructors are not discussed here, omitted ...
- };
- //The following is called:
- Cxstring string1 (24); //This is OK, pre-allocating 24 bytes of memory for cxstring
- cxstring string2 = 10; //This is OK, pre-allocating 10 bytes of memory for cxstring
- Cxstring String3; //This is not possible, because there is no default constructor, the error is: "cxstring": there is no appropriate 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 OK, in fact, the call is cxstring (int size), and size equals ' C ' ASCII code
- string1 = 2; //This is OK, pre-allocating 2 bytes of memory for cxstring
- string2 = 3; //This is OK, pre-allocating 3 bytes of memory for cxstring
- String3 = string1; //This is OK, at least compile is not a problem, but if the destructor with free to release _pstr memory pointers may be error, the complete code must overload operator "=", and in which to handle memory release
In the above code, "cxstring string2 = 10;" Why is this sentence possible? In C + +, if the constructor has only one parameter, there is a default conversion operation at compile time: Convert the data of that constructor's data type to that class object. That is, "cxstring string2 = 10;" This code, the compiler automatically converts an integral type to a Cxstring class 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 represents the size of the string memory allocation, then the second sentence of the call "cxstring string2 = 10;" And the sixth sentence "Cxstring string6 = ' C ';" is not a fish and fish, and it is easy to confuse people. Is there any way to stop this usage? The answer is to use the explicit keyword. Let's change the code above, as follows:
- Class cxstring //Use keyword explicit to display the conversion
- {
- Public
- Char *_pstr;
- int _size;
- explicit cxstring (int size)
- {
- _size = size;
- //code ibid., omitted ...
- }
- Cxstring (const char *p)
- {
- //code ibid., omitted ...
- }
- };
- //The following is called:
- Cxstring string1 (24); //This is OK.
- cxstring string2 = 10; //This is not possible because the explicit keyword cancels the implicit conversion
- Cxstring String3; //This is not possible, because there is no default constructor
- 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 not possible, in fact, the call is cxstring (int size), and size equals ' C ' ASCII code, but the explicit keyword de-implicit conversion
- string1 = 2; //This is also not possible, because the implicit conversion is canceled
- string2 = 3; //This is also not possible, because the implicit conversion is canceled
- String3 = string1; //This is also not possible, because the implicit conversion is canceled, unless the class implements an overload of the operator "="
The purpose of the explicit keyword is to prevent implicit automatic conversion of class constructors.
As already mentioned, the explicit keyword is valid only for a class constructor with one argument, and if the class constructor parameter is greater than or equal to two, the implicit conversion is not generated, so the explicit keyword is not valid. For example:
- Class Cxstring //Explicit keyword is invalid if the class constructor parameter is greater than or equal to two
- {
- Public
- Char *_pstr;
- int _age;
- int _size;
- explicit cxstring (int age, int size)
- {
- _age = age;
- _size = size;
- //code ibid., omitted ...
- }
- Cxstring (const char *p)
- {
- //code ibid., omitted ...
- }
- };
- //This time there are no explicit keywords are the same
However, there is one exception, when the explicit keyword is still valid when there are default values for parameters other than the first argument, when the constructor is called only one parameter is equivalent to the class constructor with only one argument, as in the following example:
- Class cxstring //Use keyword Explicit declaration
- {
- Public
- int _age;
- int _size;
- explicit cxstring (int age, int size = 0)
- {
- _age = age;
- _size = size;
- //code ibid., omitted ...
- }
- Cxstring (const char *p)
- {
- //code ibid., omitted ...
- }
- };
- //The following is called:
- Cxstring string1 (24); //This is OK.
- cxstring string2 = 10; //This is not possible because the explicit keyword cancels the implicit conversion
- Cxstring String3; //This is not possible, because there is no default constructor
- string1 = 2; //This is also not possible, because the implicit conversion is canceled
- string2 = 3; //This is also not possible, because the implicit conversion is canceled
- String3 = string1; //This is also not possible, because the implicit conversion is canceled, unless the class implements an overload of the operator "="
The above is a detailed introduction to the C + + explicit keyword
C + + Explicit keywords