C++ 如何判断所调用的重载函数
生活随笔
收集整理的這篇文章主要介紹了
C++ 如何判断所调用的重载函数
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
函數重載是C++的一個重要特性,但是函數重載后調用哪一個函數往往令人很困惑,參照《C++ Primer》第七章的內容總結如下:
?重載確定函數調用的步驟
1、候選函數(candidate function)
?1)、僅當形參是引用或指針時,形參是否為const 才有影響
?2)、不能基于指針本身是否為const 來實現函數重載
?3)、return 的類型不能用于判斷重載
?2、可行函數
?1)、函數形參個數匹配(默認參數也是實參)
?2)、類型匹配,包含隱式轉換的匹配
3、尋找最佳匹配
1)、精確類型匹配 > 需要轉換的匹配
2)、通過類型提升的轉換 > 其他標準轉換
我還是喜歡用實際的例子來說明問題:
1 #include <iostream> 2 #include <cstdlib> 3 4 /*=================================================================*\ 5 * 6 * 重載確定函數調用的步驟 7 * 8 * 2013/6/5 by 樊列龍 9 * 10 \*=================================================================*/ 11 12 /* 13 * 重載確定函數調用的步驟 14 * 1、候選函數(candidate function) 15 * 1)、僅當形參是引用或指針時,形參是否為const 才有影響 16 * 2)、不能基于指針本身是否為const 來實現函數重載 17 * 3)、return 的類型不能用于判斷重載 18 * 2、可行函數 19 * 1)、函數形參個數匹配(默認參數也是實參) 20 * 2)、類型匹配,包含隱式轉換的匹配 21 * 3、尋找最佳匹配 22 * 1)、精確類型匹配 > 需要轉換的匹配 23 * 2)、通過類型提升的轉換 > 其他標準轉換 24 */ 25 26 using namespace std; 27 28 29 //// 30 void funA(int &a, int &b) 31 { 32 cout << "調用:funA(int &a, int &b)" << endl; 33 } 34 35 void funA(const int &a, const int &b) 36 { 37 cout << "調用:funA(const int &a, const int &b)" << endl; 38 } 39 40 41 //// 42 void funB(int *a, int *b) 43 { 44 cout << "調用:funB(int *a, int *b)" << endl; 45 } 46 void funB(const int *a, const int *b) 47 { 48 cout << "調用:funB(const int *a, const int *b)" << endl; 49 } 50 51 //// 52 void f() 53 { 54 cout << "調用:void f()" << endl; 55 } 56 57 void f(int ) 58 { 59 cout << "調用:void f(int )" << endl; 60 } 61 62 void f(int , int i = 1 ) 63 { 64 cout << "調用:void f(int , int )" << endl; 65 } 66 67 void f(double, double d = 1.0) 68 { 69 cout << "調用:void f(double, double)" << endl; 70 } 71 72 73 /// 74 void ff(short) 75 { 76 cout << "調用:void ff(short)" << endl; 77 } 78 void ff(int) 79 { 80 cout << "調用:void ff(int)" << endl; 81 } 82 83 /// 84 void fff(long) 85 { 86 cout << "調用:void fff(long)" << endl; 87 } 88 void fff(float) 89 { 90 cout << "調用:void fff(float)" << endl; 91 } 92 93 int main() 94 { 95 int a, b; 96 const int ca = 0; 97 const int cb = 0; 98 99 funA(a,b); // 調用:funA(int &a, int &b) 100 funA(a,cb); // 調用:funA(const int &a, const int &b) 101 funA(ca,cb); // 調用:funA(const int &a, const int &b) 102 103 funB(&a,&b); // 調用:funB(int *a, int *b) 104 funB(&ca,&cb); // 調用:funB(const int *a, const int *b) 105 funB(&a,&cb); // 調用:funB(const int *a, const int *b) 106 107 108 f(5.6); // 調用:void f(double, double) 109 //f(5); // Error: 調用重載的‘f(int)’有歧義 110 //f(5,6.0); // 錯誤:調用重載的‘f(int, double)’有歧義 111 f(static_cast<double>(5),6.0);// 調用:void f(double, double) 112 f(5,static_cast<int>(6.0)); // 調用:void f(int , int ) 113 114 115 cout << "sizeof(short): " << sizeof(short) << endl; 116 cout << "sizeof(char): " << sizeof(char) << endl; 117 118 ff('a'); // 調用:void ff(int) 119 120 //double 既可以轉換為long 也可以轉換為 float,有二義性 121 //fff(3.14); // 錯誤:調用重載的‘fff(double)’有歧義 122 123 return EXIT_SUCCESS; 124 }執行結果:
1 調用:funA(int &a, int &b) 2 調用:funA(const int &a, const int &b) 3 調用:funA(const int &a, const int &b) 4 調用:funB(int *a, int *b) 5 調用:funB(const int *a, const int *b) 6 調用:funB(const int *a, const int *b) 7 調用:void f(double, double) 8 調用:void f(double, double) 9 調用:void f(int , int ) 10 sizeof(short): 2 11 sizeof(char): 1 12 調用:void ff(int) View Code?
?
轉載于:https://www.cnblogs.com/CocoonFan/archive/2013/06/05/3118240.html
總結
以上是生活随笔為你收集整理的C++ 如何判断所调用的重载函数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android中引入第三方Jar包的方法
- 下一篇: Oracle 中的SID是什么意思?有什