C ++ notes | Lesson 2 | function overloading

Lesson 2 notes C ++ function overloading

  • Function overloading (function overloading)
  • C ++ allows functions of the same name, a phenomenon known as function overloading
//eg.求绝对值
int abs(int);
long labs(long);
double fabs(double);

Note to note parameter type calling: for example, 1.0 and 1.0f

Default and function overloading

double f(double x){ return x*x;}
double f(double x, double y=0) { return x*x+y*y;}
double x(3.0), y(4.0);
//出错!由于函数有缺省参数说明,导致编译器根据f(x)调用无法静态束定应该调用f(x)还是f(x, 0.0)

//正确写法
double f(double x){ return x*x;}
double f(double x, double y) { return x*x+y*y;}
double x(3.0), y(4.0);

//正确写法
double f(double x, double y=0.0) { return x*x + y*y;}
Published 10 original articles · won praise 0 · Views 187

Guess you like

Origin blog.csdn.net/qq_45379253/article/details/104868515