3.1如果同一作用域内的几个函数名字相同但形参列表不同,我们称之为重载函数。
3.1.1不允许两个函数除了返回类型外所有的要素都相同。(这也是很好解释的)
1 #include<iostream> 2 using namespace std; 3 int fuc(int a, double b) 4 { 5 cout<<"a= "<<a<<" "<<"b= "<<b<<endl; 6 return 0; 7 } 8 void fuc(int a, double b) 9 { 10 cout<<"a= "<<a<<" "<<"b= "<<b<<endl; 11 } 12 int main(void) 13 { 14 //如果允许只有返回类型不同,那么下面这个该调用谁呢? 15 fuc(3,3.14);//因为我们完全可以不关心返回值,所以这样的调用是很常见也是合法的 16 return 0; 17 }
3.1.2 有默认参数的重载测试
#include<iostream> using namespace std; int fuc(int a, double b=3.14) { cout<<"a= "<<a<<" "<<"b= "<<b<<endl; return 0; } int fuc(int a) { cout<<"a= "<<a<<endl; return 0; } int main(void) { fuc(10);//错误,此时编译器不知道到底调用谁,所以有默认实参的重载一定要小心,尤其在后面的类当中。 return 0; }
3.1.3 忽略了顶层const的重载
1 eg1: 2 #include<iostream> 3 using namespace std; 4 int fuc(int a) 5 { 6 cout << "a= " << a << endl; 7 return 0; 8 } 9 int fuc(const int a) 10 { 11 cout << "a= " << a << endl; 12 return 0; 13 } 14 int main(void) 15 { 16 fuc(10);//错误,依旧不能重载,顶层const会被忽略,具体见前面随笔的讲解 17 return 0; 18 } 19 20 eg2: 21 void test(const int *a) 22 { 23 24 } 25 void test(int *a) 26 { 27 28 } 29 int main(void) 30 { 31 int *a=nullptr; 32 test(a);//底层const可以重载 33 return 0; 34 }
原文:http://www.cnblogs.com/yangguang-it/p/6387743.html