More effective C + + clause 5 stay alert for custom type conversion functions

Source: Internet
Author: User

1. C + + allows implicit conversions between built-in data types (such as char and Int,int and double), with detailed rules for implicit conversions between built-in types, but in any case, these are language-provided, relatively secure, and we cannot change (that is, We have no control over these things);

For custom class types, implicit conversions can be implemented through constructors with a single argument and implicit type conversion operators (the so-called "single argument" means that there can be multiple arguments, but other parameters must have a default argument in addition to the first argument)

2. For a type conversion of a custom type, there is a rule that "no translator can contain more than one ' custom conversion behavior ' (i.e., single-argument constructor, or implicit type-conversion operator)", which means If necessary, the compiler can first convert between built-in types and then call the constructor of the band-independent variable, or call the implicit type conversion operator to convert between built-in types, but it is not possible to perform two user-customized type conversions in succession!

3. The implicit type conversion operator type () means that "you need to go, you can turn", that is, because implicit conversions are too flexible, in some cases, the designer of the class does not want to behave as expected. For example, the designer defines a rational number class rational, defines operator int (), and does not define << In this case, if the statement "cout<<a;", the compiler should error to remind the designer, But actually a will be converted to int and then output, which deviates from the designer's original intention.

It is therefore better not to define implicit type conversion operators, but instead to define functions like "double todouble ()" To perform type conversions, although they are used in a slightly different way, but "can be remedied by not silently invoking functions that are not intended to be called." This is probably why the string class in the C + + standard library takes the C_STR function from an implicit type conversion operator that does not have a string to char*.

4. For a single-argument constructor, there may be more subtle errors due to implicit conversions, such as: For a Array<int> object, the following code might be correct:

array<int> A;        Array<int> b;          for (int i=0;i<; + +i)             if (A=b[i]) {}       // Note that A's [] is dropped, but if you define a array<int> constructor that takes only an int value as a parameter, the code syntax is correct .
View Code

If you want to remove the implicit type conversion attribute of the constructor for a univariate parameter, you can declare it with the EXPLICT keyword, and if the compiler does not support the EXPLICT keyword, you can add a layer between the custom class and the built-in type to encapsulate only one of the built-in type members, and use the 2 rule to achieve the same effect, such as:

class array{        public:        class  arraysize{        public:            ArraySize (int  numelements): thesize (numelements) {}         Private :             int thesize;        };        Array (ArraySize size);        ...};
View Code

Where ArraySize is referred to as the proxy class, the only drawback is that it is worthwhile to add a layer of transformation in the middle of constructing an object with a single argument.

5. The summary allows the compiler to perform implicit conversions that do more harm than benefit, so it's not necessary to provide a conversion function!

More effective C + + clause 5 stay alert for custom type conversion functions

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.