Avoiding implicit type conversions by overloading in C + +

Source: Internet
Author: User

Here's a piece of code, and if there's no unusual reason, there's nothing to see:

Class Upint {//Unlimited precision
Public://integers class
Upint ();
Upint (int value);
...
};
For an explanation of why the return value is const, see effective C + + clause 21
Const Upint operator+ (const upint& LHS, const upint& RHS);
Upint Upi1, Upi2;
...
Upint Upi3 = upi1 + upi2;
There is nothing surprising here. Both Upi1 and Upi2 are Upint objects, so the addition of them will call the upints operator function.

Now consider the following statements:

Upi3 = Upi1 + 10;
Upi3 = ten + upi2;
These statements can also run successfully. The method is to convert the number 10 to upints by creating a temporary object (see clause 19).

It's really convenient for the compiler to do this type conversion, but it's expensive to create a temporary object for type conversion, and we don't want to incur that overhead. Just as most people want to benefit from the government rather than pay for it, most C + + programmers want implicit type conversions with no temporary object overhead. But how can we do that if there is no deficit in the computing world?

Let's take a step back and realize that our goal is not to do type conversions, but to call operator with upint and int as arguments. Implicit type conversions are only a means to an end, but we do not confuse means with purpose. There is also a way to successfully operator a mixed type call, which eliminates the need for implicit type conversions. If we want to add upint and int objects, each function has a different set of parameter types by declaring the following functions to achieve this goal.

Const Upint operator+ (const upint& LHS,//Add Upint
Const upint& RHS); and Upint
Const Upint operator+ (const upint& LHS,//Add Upint
int rhs); and int
Const Upint operator+ (int LHS,//Add int and
Const upint& RHS); Upint
Upint Upi1, Upi2;
...
Upint Upi3 = upi1 + upi2; Correct, not by upi1 or upi2
Generated temporary objects
Upi3 = Upi1 + 10; Correct, not by UPI1 or 10
Generated temporary objects
Upi3 = ten + upi2; Correct, not by a or upi2
The generated temporary object.
Once you start using a function overload to eliminate type conversions, it is possible for you to declare a function and put yourself in danger:

Const Upint operator+ (int lhs, int rhs); Error!
The idea is reasonable. for upint and int types, we want to overload the operator function with all possible combinations. There are only three overloaded functions, the only ones missing are operator with two int parameters, so we want to add it.

Does it make sense? One rule in C + + is that each overloaded operator must have an argument with a user-defined type (user-defined type). int is not a user-defined type, so we cannot overload operator as a function with only this type of parameter. (without this rule, programmers would be able to change predefined operations, which would definitely introduce the program into a messy situation.) For example, attempting to overload the above operator will change the meaning of the int type addition. )

The method of avoiding temporary objects with overloading is not only used on operator functions. For example, in most programs, you want to allow the use of char* in all places where you can use a string object, and vice versa. Similarly if you are using the numerical (number) class, such as complex (see clause 35), you want the type int and double to be used anywhere in the numerical object. Therefore, any function with string, char*, and complex parameters can be overloaded to eliminate type conversions.

However, the 80-20 rule must be remembered (see article 16). There is no need to implement a large number of overloaded functions unless you have reason to believe that the overall efficiency of a program using overloaded functions can be significantly improved.

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.