C ++ explicit keywords

Source: Internet
Author: User

In section 394th of C ++ primer's 4th, I explained the keyword "explicit". below is my learning experience.

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 MyClass {public: MyClass (int num);} //. MyClass obj = 10; // OK, convert int to MyClass
In the code above, the compiler automatically converts an integer to a MyClass object, which is actually equivalent to the following operation:

MyClass temp (10); MyClass obj = temp; the above operations related to all the C ++ explicit it keywords are called "implicit conversions ".

 

 

 

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 allocates a null String of 10 bytes and calls the function directly.
String s3 = String (10); // OK allocates 10 bytes of null strings and calls the function directly.
 
The following two statements are confusing:
String s4 = 10; // It is compiled and is also an empty String that is allocated 10 bytes. implicit conversion: String temp = String (10); String sv = temp
String s5 = 'a'; // The Null String allocated with int ('A') bytes is implicitly converted to String temp = String ('A '); string sv = temp
 
S4 and s5 convert an int type and a 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 the explicit Keyword:
Class String {
Explicit String (int n); // the intention is to allocate n Bytes to the String in advance
String (const char * p); // use the C-style String p as the initialization value.
//...
}
The addition of explicit will suppress the implicit conversion of String (int n,
 
The following two statements are still correct:
String s2 (10); // OK allocates a null String of 10 bytes.
String s3 = String (10); // OK allocates a null String of 10 bytes.
 
The following two statements are not allowed:
String s4 = 10; // implicit conversion is not allowed because the compilation fails.
String s5 = 'a'; // implicit conversion is not allowed because the compilation fails.
 
Therefore, in some cases, explicit can effectively prevent errors or misunderstandings caused by implicit conversions of constructors.

 

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.