作用:函数名可以相同,提高复用性。
喊出重载满足条件:
- 同一个作用域;
- 函数名相同
- 函数参数类型不同或者个数不同或者顺序不同;
#include<iostream> using namespace std; //函数重载需要函数在同一个作用域下 void func() { cout << "调用func()" << endl; } void func(int a) { cout << 调用func(int a)float调用func(float a)int a, b) { cout << 调用func(int a,float b)float a,1)">调用func(float a,int b)int b,1)"> c) { cout << endl; } main() { func(); func(1); func(1.2f1,1.2f,1)">2,1)">3); system(pause"); return 0; }
输出:
函数重载注意事项:
- 引用作为从重载条件
- 函数重载碰到函数默认参数
#include<iostream> int&调用func(int &a)const 调用func(const int &a) endl; } main() { int a = 10; func(a);这里调用的是func(int &a) func(10);这里调用的是func(const int &a) system(; }
输出:
#include<iostream> int b = 20) { cout << main() { func(; }
这种情况下,func(10)不清楚会调用哪一个函数,报错。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。