< turn >c++ Explicit keyword explanation

Source: Internet
Author: User

To transfer 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:

classCxstring//no class declaration using the explicit keyword, which is implicitly declared by default{   Public:      Char*_pstr; int_size; Cxstring (intsize) {_size= size;//the preset size of the string_pstr =malloc(Size +1);//allocating memory for stringmemset (_PSTR,0, size +1); } cxstring (Const Char*p) {intSize =strlen (P); _pstr=malloc(Size +1);//allocating memory for stringstrcpy (_PSTR, p);//Copy String_size =strlen (_PSTR); }      //destructors are not discussed here, omitted ...}; //here is the call:cxstring string1 ( -);//This is OK, pre-allocating 24 bytes of memory for the cxstringCxstring string2 =Ten;//This is OK, pre-allocating 10 bytes of memory for the cxstringCxstring String3;//This is not possible because there is no default constructor and the error is: "cxstring": there is no appropriate default constructor availableCxstring 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 codestring1 =2;//This is also OK, pre-allocating 2 bytes of memory for the cxstringstring2 =3;//This is also OK, pre-allocating 3 bytes of memory for the cxstringString3 = string1;//This is OK, at least compile is no 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 (ten);  or  cxstring temp (ten);  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//Using the keyword explicit class declaration, display conversion {Public:char *_pstr;      int _size;          explicit cxstring (int size) {_size = size;      Code ibid., omitted ...}  cxstring (const char *p) {//code ibid., omitted ...}        };     Here is the call: Cxstring string1 (24);    This is ok cxstring string2 = 10;         This is not possible, because the explicit keyword cancellation of 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 cancellation of the implicit conversion 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 when a 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 ...      }  };  

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 ...      }  };        Here is the call:        cxstring string1 ();     This is ok      cxstring string2 = ten;    This is not possible, because the explicit keyword cancellation of 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 cancellation of the implicit conversion      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.

< turn >c++ Explicit keyword explanation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.