函数重载与函数默认参数

 1 /*函数重载的原理*/
 2 
 3 // c中
 4 
 5 // 参数的个数参数的类型不同 顺序不同 与返回值无关
 6 void go(int a)
 7 {
 8     std::cout << a;
 9 }
10 
11 void go(double a)
12 {
13     std::cout << a;
14 }
15 
16 void go(double a,int b)
17 {
18     std::cout << a << b;
19 }
20 
21 void go(int a,double b)
22 {
23     std::cout << a << b;
24 }
25 
26 void main()
27 {
28     void (*pgo1)(int a)=go;// go 根据参数委托到不同的地址
29     void (*pgo2)(double a)=go;
30     void (*pgo3)(double a ,int b)=go;
31     void (*pgo4)(int a,double b)=go;
32     printf("%p\n",pgo1);
33     printf("%p\n",pgo2);
34     printf("%p\n",pgo3);
35     printf("%p\n",pgo4);
36 }
37 
38 //-----------------------------------------------------
39 
40 /*函数的默认参数*/
41 
42 // 默认参数必须放在右边,默认参数中间不允许出现不默认的
43 void print(int c,int a=1,int b = 3)
44 {
45     std::cout << a << b << c << std::endl;
46 }
47 
48 void print(int c)
49 {
50     std::cout << c << std::endl;
51 }
52 
53 void main()
54 {
55     //print(1,2,3);
56     
57     // 函数指针没有默认参数,必须全部输入数据
58     // 函数重载与函数默认参数冲突需要你输入的参数类型不一个,个数不一样,
59     // 顺序不一样不会出现问题,否则一定报错
60 
61     void (*pt1)(int c,int a,int b) = print;
62     pt1(100,1,3);// 函数指针调用,没有用默认的
63 }

 

posted on 2015-05-26 09:39  Dragon-wuxl  阅读(144)  评论(0)    收藏  举报

导航