C++中函數(shù)匹配機(jī)制詳解
首先,編譯器會(huì)確定候選函數(shù)然后確定可行函數(shù)可行函數(shù),再?gòu)目尚泻瘮?shù)中進(jìn)一步挑選
候選函數(shù):重載函數(shù)集中的函數(shù)
可行函數(shù):可以調(diào)用的函數(shù)
最后進(jìn)行尋找最佳匹配
有以下幾種規(guī)則
1.該函數(shù)的每個(gè)實(shí)參的匹配都不劣于其他可行函數(shù)
2.至少有一個(gè)實(shí)參的匹配優(yōu)于其他可行函數(shù)的匹配
3.滿足上面兩種要求的函數(shù)有且只有一個(gè)
如果上面三個(gè)要求都沒(méi)滿足,則出現(xiàn)二義性
一些演示
各有一個(gè)精確匹配的實(shí)參,編譯器報(bào)錯(cuò),不滿足條件3
error
void func(int a,int b)
{
cout << "(int,int)" << endl;
}
void func(double a, double b = 3.14)
{
cout <<"(double,double = 3.14)" << endl;
}
int main()
{
func(2.3,5);
}調(diào)用函數(shù)類型轉(zhuǎn)換優(yōu)先級(jí),依次遞減
1.精確匹配
包含三種
(1)實(shí)參形參類型匹配
(2)從數(shù)組或者函數(shù)轉(zhuǎn)到對(duì)應(yīng)的指針
(3)添加或者刪除頂層const
2.通過(guò)const轉(zhuǎn)換實(shí)現(xiàn)匹配
3.通過(guò)類型提升匹配
4.通過(guò)算術(shù)類型轉(zhuǎn)換或者指針轉(zhuǎn)換的匹配
5.通過(guò)類 類型轉(zhuǎn)換實(shí)現(xiàn)的匹配
小整形一般提升為int或者long,即便他的面量很小
void func(int a)
{
cout << "(int)" << endl;
}
void func(short a)
{
cout << "(short)" << endl;
}
int main()
{
func(12);
func('a');
}運(yùn)行結(jié)果

所有算數(shù)類型級(jí)別轉(zhuǎn)換都一樣
error
void func(double a)
{
cout << "(double)" << endl;
}
void func(float a)
{
cout << "(float)" << endl;
}
int main()
{
func(3.14);
}不能重載const 和非const兩個(gè)版本,但是引用可以
關(guān)于引用:非const可以升級(jí)為const,但是const不能降級(jí)為非const
若有兩種版本——const and not const,會(huì)根據(jù)傳入的參數(shù)自動(dòng)匹配
void func(const int &a)
{
cout << "(const int&)" << endl;
}
void func(int &a)
{
cout << "(int&)" << endl;
}
int main()
{
const int a = 3;
int b = 4;
func(a);
func(b);
func(5);
}運(yùn)行結(jié)果

指針的情況于引用類似:
如果兩個(gè)函數(shù)唯一的區(qū)別是他們指向的對(duì)象是常量或非常量,則編譯器根據(jù)實(shí)參選擇函數(shù)。
演示
void func(const int *a)
{
cout << "(const int *)" << endl;
}
void func(int *)
{
cout << "(int *)" << endl;
}
int main()
{
int a = 3;
int *pa = &a;
const int *c_pa = &a;
const int b = 4;
const int *pb = &b;
func(pa);
func(c_pa);
func(pb);
}運(yùn)行結(jié)果

上面提到過(guò)的一些重載
using namespace std;
void func(int a)
{
cout << "(int)" << endl;
}
void func(double a)
{
cout << "(double)" << endl;
}
void func(int a,int b)
{
cout << "(int,int)" << endl;
}
void func(double a, double b = 3.14)
{
cout <<"(double,double = 3.14)" << endl;
}
void func(short a)
{
cout << "(short)" << endl;
}
void func(float a)
{
cout << "(float)" << endl;
}
void func(const int &a)
{
cout << "(const int&)" << endl;
}
void func(int &a)
{
cout << "(int&)" << endl;
}
void func(const int *a)
{
cout << "(const int *)" << endl;
}
void func(int *)
{
cout << "(int *)" << endl;
}到此這篇關(guān)于C++中函數(shù)匹配機(jī)制詳解的文章就介紹到這了,更多相關(guān)C++函數(shù)匹配內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
一文詳解C++子類函數(shù)為什么不能重載父類函數(shù)
這篇文章主要介紹了一文詳解C++子類函數(shù)為什么不能重載父類函數(shù),文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容戒殺,具有一定的參考價(jià)值,需要的朋友可以參考一下2022-09-09
淺談c++性能測(cè)試工具之計(jì)算時(shí)間復(fù)雜度
有時(shí)候除了測(cè)量算法的具體性能指數(shù),我們也會(huì)希望測(cè)試出算法的時(shí)間復(fù)雜度,以便我們對(duì)待測(cè)試的算法的性能有一個(gè)更加直觀的了解。本文將介紹c++性能測(cè)試工具之計(jì)算時(shí)間復(fù)雜度。2021-06-06
C++中rapidjson組裝map和數(shù)組array的代碼示例
今天小編就為大家分享一篇關(guān)于C++中rapidjson組裝map和數(shù)組array的代碼示例,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-04-04
探討C++中不能聲明為虛函數(shù)的有哪些函數(shù)
下面小編就為大家?guī)?lái)一篇探討C++中不能聲明為虛函數(shù)的有哪些函數(shù)。希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧,祝大家游戲愉快哦2017-01-01

