Explicit constructor _ function

Source: Internet
Author: User
by default, the constructor of only one parameter also defines an implicit conversion that converts data of the constructor's corresponding data type to that class object, as shown here:Class String {string (const char* p); Use C-style string p as initialization value//...} String S1 = "Hello"; OK implicit conversions, equivalent to string S1 = string ("Hello"); but sometimes this implicit conversion may not be required, as follows:Class String {string (int n);//is intended to allocate n bytes to string strings (const char* p);   Use C-style string p as initialization value//...}    The following two kinds of writing are more normal: String S2 (10); OK allocates a 10-byte empty string s3 = String (10); OK allocate 10 bytes of empty string The following two kinds of writing are more puzzled: string s4 = 10; Compilation passed, is also allocated 10 bytes of empty string s5 = ' a '; The compilation passed, allocating an int (' a ') byte of an empty string S4 and S5 respectively, an int and a char, implicitly converting to an empty string allocating several bytes, is easily misleading. To avoid this error, we can declare the conversion of the display, using the ExplicitKeywords: class String { ExplicitString (int n);  The intention is to allocate n bytes to string strings (const char* p) in advance; Use C-style string p as initialization value//...} Plus Explicit,Suppresses the implicit conversion of string (int n), the following two ways are still correct: string S2 (10); OK allocates a 10-byte empty string s3 = String (10); OK allocate 10 bytes of empty string The following two kinds of writing are not allowed: string s4 = 10; Compilation does not allow implicit conversion of String S5 = ' a '; Compilation does not allow implicit conversions so, at some point, ExplicitCan effectively prevent errors or misunderstandings caused by implicit conversions of constructors

----------------------------------------------------------
Explicit only works on constructors to suppress implicit conversions. Such as:
Class A {
A (int a);
};
int Function (a);

When a Function (2) is invoked, 2 is implicitly converted to type A. This is often not the result that programmers want, so to avoid it, you can write:

Class A {
Explicit a (int a);
};
int Function (a);

Thus, when the function (2) is invoked, the compiler gives an error message (unless the function has an overloaded form with an int), which avoids the error if the programmer is unaware.

Summary: Explicit only works on constructors to suppress implicit conversions.
Original post address: http://www.cnblogs.com/cutepig/archive/2009/01/14/1375917.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.