-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path12_函数_03_函数重载.cpp
71 lines (54 loc) · 1.3 KB
/
12_函数_03_函数重载.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>
using namespace std;
// 函数重载
/*
作用:函数名可以相同,提高复用性
条件:
- 同一个作用域下
- 函数名称相同
- 函数参数类型不同或个数不同或顺序不同
注意:函数的返回值不可以作为函数重载的条件
*/
void func()
{
cout << "调用了func" << endl;
}
void func(int a)
{
cout << "调用了func(int a)" << endl;
}
void func(double a)
{
cout << "调用了func(double a)" << endl;
}
// 函数重载遇到引用
// 有无 const 可以作为重载条件
void func2(int &a) //func2(10) 等效于 int &a = 10; 不合法,故不使用本实现
{
cout << "调用了func2(int &a)" << endl;
}
void func2(const int &a) //func2(10) 等效于 const int &a = 10; 合法,使用本实现
{
cout << "调用了func2(const int &a)" << endl;
}
// 函数重载遇到默认参数
void func3(int a)
{
cout << "调用了func3(int a)" << endl;
}
void func3(int a, int b = 10)
{
cout << "调用了func3(int a, int b = 10)" << endl;
}
int main()
{
func();
func(10);
func(3.14);
int a = 10;
func2(a); // 对应 int &a
func2(10); // 对应 const int &a
// func3(10); // 出错,同时满足两种实现的参数情况,出现二义性,需要避免
func3(10, 20); // 可以执行
return 0;
}