C ++ explicit Constructor

Source: Internet
Author: User
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 Null String string S3 = string (10) allocated with 10 bytes; // OK: String S4 = 10; // It is also an empty string S5 = 'A' allocated with 10 bytes. // It is compiled with an int ('A ') each byte's Null String S4 and S5 convert an int and 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 Explicit Keyword: Class string { ExplicitString (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 //...} Add Explicit , The implicit conversion of string (int n) is restrained. The following two statements are still correct: String S2 (10 ); // OK The Null String string S3 = string (10) allocated with 10 bytes; // OK is allocated with 10 bytes. The following two types of empty strings cannot be written: String S4 = 10; // compilation failed. implicit conversion of string S5 = 'A' is not allowed. // compilation failed. implicit conversion is not allowed. Therefore, in some cases, Explicit It can effectively prevent errors or misunderstandings caused by implicit conversions of constructors.

--------------------------------------------------------
explicit only works for constructors and is used to suppress implicit conversions. For example:
Class A {
A (int A);
};
int function (a );
when function (2) is called, type 2 is implicitly converted to type. This situation is often not the result of the Program . To avoid this, you can write it like this:
Class A {
explicit a (int A);
};
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.

Refer:
Http://blog.csdn.net/smilelance/archive/2007/03/14/1528737.aspx
Http://topic.csdn.net/t/20040509/15/3046021.html

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.