Detailed explanation of C++ function overloading

http://blog.csdn.net/zhanghow/article/details/53588458

In actual development, sometimes we need to implement several functions with similar functions, but with different details. For example, if we want to exchange the values ​​of two variables, these two variables have multiple types, which can be int, float, char, bool, etc. We need to pass the address of the variable into the function through the parameter. In the C language, programmers often need to design three functions with different names, and their function prototypes are similar to the following:

 
   
  1. void  swap1 ( int  * a ,  int  * b );  // swap the value of the int variable
  2. void  swap2 ( float  * a ,  float  * b );  //Swap the value of float variable
  3. void  swap3 ( char  * a ,  char  * b );  //Swap the value of the char variable
  4. void  swap4 ( bool  * a ,  bool  * b );  // swap the value of the bool variable
But in C++, this is completely unnecessary. C++ allows multiple functions to have the same name as long as their parameter lists are different. This is called function overloading . With overloading, a function name can be used for multiple purposes.

The parameter list is also called the parameter signature, including the type of parameters, the number of parameters and the order of parameters. As long as there is one difference, it is called parameter list difference.

[Example] Swap the values ​​of variables of different types with the help of function overloading:
 
   
  1. #include <iostream>
  2. using namespace std;
  3. // swap the value of the int variable
  4. void Swap(int *a, int *b){
  5. int temp = *a;
  6. *= *b;
  7. *= temp;
  8. }
  9. // swap the value of the float variable
  10. void Swap(float *a, float *b){
  11. float temp = *a;
  12. *= *b;
  13. *= temp;
  14. }
  15. // swap the value of the char variable
  16. void Swap(char *a, char *b){
  17. char temp = *a;
  18. *= *b;
  19. *= temp;
  20. }
  21. // swap the value of the bool variable
  22. void Swap(bool *a, bool *b){
  23. char temp = *a;
  24. *= *b;
  25. *= temp;
  26. }
  27. int main(){
  28. //交换 int 变量的值
  29. int n1 = 100, n2 = 200;
  30. Swap(&n1, &n2);
  31. cout<<n1<<", "<<n2<<endl;
  32. //交换 float 变量的值
  33. float f1 = 12.5, f2 = 56.93;
  34. Swap(&f1, &f2);
  35. cout<<f1<<", "<<f2<<endl;
  36. //交换 char 变量的值
  37. char c1 = 'A', c2 = 'B';
  38. Swap(&c1, &c2);
  39. cout<<c1<<", "<<c2<<endl;
  40. //交换 bool 变量的值
  41. bool b1 = false, b2 = true;
  42. Swap(&b1, &b2);
  43. cout<<b1<<", "<<b2<<endl;
  44. return 0;
  45. }
运行结果:
200, 100
56.93, 12.5
B, A
1, 0

本例之所以使用 Swap 这个函数名,而不是使用 swap ,是因为 C++ 标准库已经提供了交换两个变量的值的函数,它的名字就是 swap ,位于 algorithm 头文件中,为了避免和标准库中的 swap 冲突,本例特地将 S 大写。

既然标准库已经提供了 swap() 函数,本例为何又要自己实现一遍呢,这不是费力不讨好吗?交换两个变量的值是一个经典且实用的函数重载案例,本例这样做仅仅是为了教学演示,并不是要替代标准库中的 swap(),读者在以后的编码过程中也应该坚持使用标准库中的 swap()。

通过本例可以发现,重载就是在一个作用范围内(同一个类、同一个命名空间等)有多个名称相同但参数不同的函数。重载的结果是让一个函数名拥有了多种用途,使得命名更加方便(在中大型项目中,给变量、函数、类起名字是一件让人苦恼的问题),调用更加灵活。

在使用重载函数时,同名函数的功能应当相同或相近,不要用同一函数名去实现完全不相干的功能,虽然程序也能运行,但可读性不好,使人觉得莫名其妙。

注意,参数列表不同包括参数的个数不同、类型不同或顺序不同,仅仅参数名称不同是不可以的。函数返回值也不能作为重载的依据。

函数的重载的规则:
  • 函数名称必须相同。
  • 参数列表必须不同(个数不同、类型不同、参数排列顺序不同等)。
  • 函数的返回类型可以相同也可以不相同。
  • 仅仅返回类型不同不足以成为函数的重载。

C++ 是如何做到函数重载的

C++代码在编译时会根据参数列表对函数进行重命名,例如 void Swap(int a, int b) 会被重命名为 _Swap_int_int void Swap(float x, float y) 会被重命名为 _Swap_float_float 。当发生函数调用时,编译器会根据传入的实参去逐个匹配,以选择对应的函数,如果匹配失败,编译器就会报错,这叫做 重载决议( Overload Resolution
不同的编译器有不同的重命名方式,这里仅仅举例说明,实际情况可能并非如此。
从这个角度讲,函数重载仅仅是语法层面的,本质上它们还是不同的函数,占用不同的内存,入口地址也不一样

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325482076&siteId=291194637