網站首頁 編程語言 正文
首先,編譯器會確定候選函數然后確定可行函數可行函數,再從可行函數中進一步挑選
候選函數:重載函數集中的函數
可行函數:可以調用的函數
最后進行尋找最佳匹配
有以下幾種規則
1.該函數的每個實參的匹配都不劣于其他可行函數
2.至少有一個實參的匹配優于其他可行函數的匹配
3.滿足上面兩種要求的函數有且只有一個
如果上面三個要求都沒滿足,則出現二義性
一些演示
各有一個精確匹配的實參,編譯器報錯,不滿足條件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); }
調用函數類型轉換優先級,依次遞減
1.精確匹配
包含三種
(1)實參形參類型匹配
(2)從數組或者函數轉到對應的指針
(3)添加或者刪除頂層const
2.通過const轉換實現匹配
3.通過類型提升匹配
4.通過算術類型轉換或者指針轉換的匹配
5.通過類 類型轉換實現的匹配
小整形一般提升為int或者long,即便他的面量很小
void func(int a) { cout << "(int)" << endl; } void func(short a) { cout << "(short)" << endl; } int main() { func(12); func('a'); }
?運行結果
所有算數類型級別轉換都一樣
error
void func(double a) { cout << "(double)" << endl; } void func(float a) { cout << "(float)" << endl; } int main() { func(3.14); }
不能重載const 和非const兩個版本,但是引用可以?
關于引用:非const可以升級為const,但是const不能降級為非const
若有兩種版本——const and not const,會根據傳入的參數自動匹配
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); }
運行結果
?指針的情況于引用類似:
如果兩個函數唯一的區別是他們指向的對象是常量或非常量,則編譯器根據實參選擇函數。
演示
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); }
運行結果
?上面提到過的一些重載
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; }
原文鏈接:https://blog.csdn.net/JAN6055/article/details/122841019
相關推薦
- 2022-04-04 Python進制轉換用法詳解_python
- 2024-03-17 為解決Win11子系統的Ubuntu被刪除后,重新安裝出現找不到系統路徑問題,無法正常安裝
- 2022-07-20 C語言通過三步翻轉法實現單詞倒置詳解_C 語言
- 2023-02-26 詳解Golang時間處理的踩坑及解決_Golang
- 2022-10-05 Python實現單例模式的五種寫法總結_python
- 2022-11-04 關于Python?列表的索引取值問題_python
- 2022-07-20 React使用有限狀態機的實現示例_React
- 2022-03-16 C#?使用Fluent?API?創建自己的DSL(推薦)_C#教程
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支