c++ 函数重载

  • 在c++中,当函数名称相同时,根据其参数类型、个数、顺序来进行重载,其本质是名字改编(name mangling)
 1 #include<iostream>
 2 //c本身是不支持函数重载的
 3 //c语言中的函数名都是唯一的
 4 
 5 using std::endl;
 6 using std::cout;
 7 
 8 #ifdef _cplusplus//使用宏定义 _cplusplus,以及extern "C"结合,可以进行c和c++进行混合编程
 9 extern "C" {//此返回内不会进行函数重载,本句意义在于,在此函数内的调用方式同c一致
10 #endif
11 
12 
13     int add(int a, int b)
14     {
15         return a + b;
16     }
17 
18 #ifdef _cplusplus
19 }//end of extern "C"
20 #endif
21 
22 
23 int add(int a, int b, int c)
24 {
25     return a + b + c;
26 }
27 
28 int add(double a, double b, double c)
29 {
30     return a + b + c;
31 }
32 
33 void func1(void)
34 {
35 }
36 
37 void func2(int idx)
38 {
39 }
40 int main()
41 {
42     int x = 1;
43     int y = 2;
44     int z = 3;
45     double m = 10;
46     double n = 20;
47     double l = 30;
48     cout << add(x, y) << endl;
49     cout << add(x, y, z) << endl;
50     cout << add(m, n, l) << endl;
51     system("pause");
52     return 0;
53 }

 

posted on 2017-07-03 12:57  李兆祥  阅读(122)  评论(0)    收藏  举报

导航