建设网站纳什么税,wordpress 悬浮联系,即将开网的平台,注册网易免费邮箱C系列-函数重载 函数重载函数重载的条件函数重载注意事项引用作为重载函数重载遇到默认参数 函数重载
函数名可以相同#xff0c; 提高复用性
函数重载的条件
同一个作用域下函数名相同函数参数不同 – 参数个数不同 – 参数顺序不同 – 参数类型不同不可以使用返回值作为重… C系列-函数重载 函数重载函数重载的条件函数重载注意事项引用作为重载函数重载遇到默认参数 函数重载
函数名可以相同 提高复用性
函数重载的条件
同一个作用域下函数名相同函数参数不同 – 参数个数不同 – 参数顺序不同 – 参数类型不同不可以使用返回值作为重载的条件
code:#includeiostreamusing namespace std;void test(){cout void test() endl;}void test(int a){cout void test(int a) endl;}void test(int a, float b){cout void test(int a, float b) endl;}void test(float a, int b){cout void test(float a, int b) endl;}void main(){test();test(100);test(100, 3.14);test(3.14, 100);system(pause);}
result:void test()void test(int a)void test(int a, float b)void test(float a, int b)函数重载注意事项
引用作为重载
参数可以分为const和非const。
code:#includeiostreamusing namespace std;void test(int a){cout void test(int a) endl;}void test(const int a){cout void test(const int a) endl;}void main(){int a 10;test(a);test(10); // 当执行void test(int a) 则为int a10会出错const int a10正常system(pause);}
result:
void test(int a)
void test(const int a)函数重载遇到默认参数
code:
#includeiostream
using namespace std;void test(int a, int b 10)
{cout void test(int a, int b 10) endl;
}
void test(int a)
{cout void test(int a) endl;
}void main()
{//test(666); // 报错不知道执行哪一个test(20, 30);system(pause);
}result:void test(int a, int b 10)