網站首頁 編程語言 正文
一、缺省參數概念
缺省參數是聲明或定義函數時為函數的參數指定一個默認值。在調用該函數時,如果沒有指定實參則采用該默認值,否則使用指定的實參
#include<iostream> using namespace std; void TestFunc(int a = 0)//參數缺省值 { cout << a << endl; } int main() { TestFunc();//沒有指定實參,使用缺省值 TestFunc(10);//指定實參,使用實參 return 0; }
有什么用?
比如在 C 語言中有個很苦惱的問題是寫棧時,不知道要開多大的空間,之前我們是如果棧為空就先開 4 塊空間,之后再以 2 倍走,如果我們明確知道要很大的空間,那么這樣就只能一點一點的接近這塊空間,就太 low 了。但如果我們使用缺省,明確知道不需要太大時就使用默認的空間大小,明確知道要很大時再傳參
#include<iostream> using namespace std; namespace WD { struct Stack { int* a; int size; int capacity; }; } using namespace WD; void StackInit(struct Stack* ps) { ps->a = NULL; ps->capacity = 0; ps->size = 0; } void StackPush(struct Stack* ps, int x) { if(ps->size == ps->capacity) { //ps->capacity *= 2;//err ps->capacity == 0 ? 4 : ps->capacity * 2;//這里就必須寫一個三目 } } void StackInitCpp1(struct Stack* ps, int defaultCP) { ps->a = (int*)malloc(sizeof(int) * defaultCP); ps->capacity = 0; ps->size = defaultCP; } void StackInitCpp2(struct Stack* ps, int defaultCP = 4)//ok { ps->a = (int*)malloc(sizeof(int) * defaultCP); ps->capacity = 0; ps->size = defaultCP; } int main() { //假設明確知道這里至少需要100個數據到st1 struct Stack st1; StackInitCpp1(&st1, 100); //假設不知道st2里需要多少個數據 ———— 希望開小點 struct Stack st2; StackInitCpp2(&st1);//缺省 return 0; }
二、缺省參數分類
全缺省參數?
void TestFunc(int a = 10, int b = 20, int c = 30) { cout << "a = " << a << endl; cout << "b = " << b << endl; cout << "c = " << c << endl; cout << endl; } int main() { //非常靈活, TestFunc(); TestFunc(1); TestFunc(1, 2); TestFunc(1, 2, 3); //TestFunc(1, , 3);//err,注意它沒辦法實現b不傳,只傳a和b,也就是說編譯器只能按照順序傳 return 0; }
注意:
全缺省參數只支持順序傳參
半缺省參數?
//void TestFunc(int a, int b = 10, /*int f, - err*/ int c = 20);//err,void TestFunc(int a, int b = 10, /*int f, int x = y, -> err*/ int c = 20){cout << "a = " << a << endl;cout << "b = " << b << endl;cout << "c = " << c << endl;cout << endl;}int main(){//TestFunc();//err,至少得傳一個,這是根據形參有幾個非半缺省參數確定的TestFunc(1);TestFunc(1, 2);TestFunc(1, 2, 3);return 0;}//void TestFunc(int a, int b = 10, /*int f, - err*/ int c = 20);//err, void TestFunc(int a, int b = 10, /*int f, int x = y, -> err*/ int c = 20) { cout << "a = " << a << endl; cout << "b = " << b << endl; cout << "c = " << c << endl; cout << endl; } int main() { //TestFunc();//err,至少得傳一個,這是根據形參有幾個非半缺省參數確定的 TestFunc(1); TestFunc(1, 2); TestFunc(1, 2, 3); return 0; }
//a.hvoid TestFunc(int a = 10);//a.cppvoid TestFunc(int a = 20){}
注意:
- 半缺省參數必須從右往左依次來給出,且不能間隔著給
- 缺省參數不能在函數聲明和定義中同時出現
- 缺省值必須是常量或者全局變量
- C 語言不支持缺省
總結
原文鏈接:https://blog.csdn.net/wh128341/article/details/122390474
相關推薦
- 2022-04-06 如何將Python編譯成C語言_python
- 2022-04-20 Android實現將View轉化為圖片并保存到本地_Android
- 2023-04-01 Pytorch中關于F.normalize計算理解_python
- 2022-11-20 React?跨端動態化核心技術實例分析_React
- 2022-09-30 關于react中useCallback的用法_React
- 2022-06-23 巧妙使用python?opencv庫玩轉視頻幀率_python
- 2023-01-06 Linux下find?命令的?7?種用法_linux shell
- 2022-02-11 SQL server 數據庫導入(附加)和分離 && 數據庫分離之后位置 &
- 最近更新
-
- 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同步修改后的遠程分支