Explicit constructor (display constructor)

Source: Internet
Author: User

By default, a constructor of only one parameter also defines an implicit conversion that converts data of the data type of that constructor to that class object, as follows:

[CPP]View Plaincopy
    1. Class String
    2. {
    3. String (const char* p) //with C-style string p as the initial value
    4. //........  
    5. }
    6. String S1 = "Hello"; //ok, implicit conversion, equivalent to string s1 = string (' Hello ')


However, there may be times when this implicit conversion is not required, as follows:

[CPP]View Plaincopy
    1. Class String
    2. {
    3. string (int n) //intended to be pre-allocated n bytes to a string
    4. String (const char* p); //Use C-style string p as the initial value
    5. //........  
    6. }

The following two types of notation are normal:

[CPP]View Plaincopy
    1. String S2 (10); //ok, which is an empty string that allocates 10 bytes
    2. String s3 = String (10); //ok, which is an empty string that allocates 10 bytes

But here are two ways to make us wonder:

[CPP]View Plaincopy
    1. String S4 = 10; //Compile through, that is, allocate 10 bytes of empty string
    2. String S5 = ' a '; //Compile through, allocate an empty string of int (' a ') bytes

S4 and S5, respectively, are an int and char, implicitly converted to an empty string that allocates several bytes, which is easily misleading.

To avoid this error, we can declare the transformation shown, that is, using the explicit keyword:

[CPP]View Plaincopy
    1. Class String
    2. {
    3. <span style="color: #cc0000;" >explicit</span> string (int n) //intended to be pre-allocated n bytes to a string
    4. String (const char* p); //Use C-style string p as the initial value
    5. //........  
    6. }

The implicit conversion of string (int n) is suppressed by adding explicit.

The following two types of notation are still correct:

[CPP]View Plaincopy
    1. String S2 (10); //ok, which is an empty string that allocates 10 bytes
    2. String s3 = String (10); //ok, which is an empty string that allocates 10 bytes


However, the previous two formulations cannot be compiled, for example:

[CPP]View Plaincopy
    1. String S4 = 10; //compilation does not pass, implicit conversions are not allowed
    2. String S5 = ' a '; //compilation does not pass, implicit conversions are not allowed


So at some point, explicit can effectively prevent errors or misunderstandings caused by implicit conversions of constructors.

For example, further explanation:

explicit only works on constructors to suppress implicit conversions , as follows:

[CPP]View Plaincopy
    1. Class A
    2. {
    3. A (int a);
    4. };
    5. int Function (a a);


When function (2) is called, it is implicitly converted to type A. This situation is often not the result we want, so to avoid this situation, we can write:

[CPP]View Plaincopy
    1. Class A
    2. {
    3. explicit A (int a);
    4. };
    5. int Function (a a);


Thus, when the function (2) is called, the compiler gives the wrong information (unless the function has an overloaded form with an int parameter), which avoids errors that we do not know about.

Explicit constructor (display constructor)

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.