Ambiguous errors in C + +

Source: Internet
Author: User
Tags types of functions

Description: Recently read a lot of C + + books, the head is big, feeling more deep is a lot of things or mysteryof, their own in the object-oriented accumulation is not deep enough to read more difficult. The back is ready to do more hands-on, every day from a few small details to start, slowly fix yourself some bad programming habits.

Recently read a lot of C + + books, the head is big, the feeling is more deep is a lot of things or mysteryof, their own in the object-oriented accumulation is not deep enough to read more difficult. The back is ready to do more hands-on, every day from a few small details to start, slowly fix yourself some bad programming habits. Before because in the company is busy, there is little time to learn the shell programming and other knowledge points to summarize, this period of time can also be done a bit.

C + + Ambiguity in the belief that we often encounter, of course, often in debugging errors when the original trap is because of their own ambiguity caused. Look at the following code:

#include <iostream>void func(int var){}void func(float var){}using namespace std;int main(int argc, char *argv[]) {    func(2.5);}

A programmer with some experience will certainly be able to guess the results of the compilation (call to ' func ' is ambiguous), because the 2.5 data type stored by default in C + + is double, and the overloaded function arguments are int and float,double can be converted to int and float , there will be ambiguous errors. If the code is modified to:

#include <iostream>void func(int var){}void func(double var){}using namespace std;int main(int argc, char *argv[]) {    float tmp = 2.5;    func(tmp);}

This compiler does not error, although float can be converted to int and double two types, float to int back loss of certain data accuracy, and float to double does not lose data precision, so the compiler made a decision, For double-to-int and float two types, data accuracy is lost, so the compiler does not have to. This is a more typical floating-point problem, it warns that when designing a function, you should use a double type whenever a floating-point number is involved, even if others use float to invoke it.

Function overloading can also be ambiguous, there are many ways to avoid this situation, one is to use a template instead of overloading, especially for the same processing algorithm, only different data types of functions, if the processing algorithm is different, as far as possible to ensure that the parameters of the function are different (can be defined to join the useless parameters).

void func(double db, int){};void func(int db){};

There are many ambiguous questions, not only in the function overload, in fact, a little regret that there is no time to choose the compiling principle of this course, later if you encounter similar problems come and share it.

Ambiguous errors in C + +

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.