C++的缺省參數(shù)你了解嘛
一、缺省參數(shù)概念
缺省參數(shù)是聲明或定義函數(shù)時為函數(shù)的參數(shù)指定一個默認(rèn)值。在調(diào)用該函數(shù)時,如果沒有指定實(shí)參則采用該默認(rèn)值,否則使用指定的實(shí)參
#include<iostream> using namespace std; void TestFunc(int a = 0)//參數(shù)缺省值 { cout << a << endl; } int main() { TestFunc();//沒有指定實(shí)參,使用缺省值 TestFunc(10);//指定實(shí)參,使用實(shí)參 return 0; }
有什么用
比如在 C 語言中有個很苦惱的問題是寫棧時,不知道要開多大的空間,之前我們是如果棧為空就先開 4 塊空間,之后再以 2 倍走,如果我們明確知道要很大的空間,那么這樣就只能一點(diǎn)一點(diǎn)的接近這塊空間,就太 low 了。但如果我們使用缺省,明確知道不需要太大時就使用默認(rèn)的空間大小,明確知道要很大時再傳參
#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() { //假設(shè)明確知道這里至少需要100個數(shù)據(jù)到st1 struct Stack st1; StackInitCpp1(&st1, 100); //假設(shè)不知道st2里需要多少個數(shù)據(jù) ———— 希望開小點(diǎn) struct Stack st2; StackInitCpp2(&st1);//缺省 return 0; }
二、缺省參數(shù)分類
全缺省參數(shù)
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,注意它沒辦法實(shí)現(xiàn)b不傳,只傳a和b,也就是說編譯器只能按照順序傳 return 0; }
注意:
全缺省參數(shù)只支持順序傳參
半缺省參數(shù)
//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,至少得傳一個,這是根據(jù)形參有幾個非半缺省參數(shù)確定的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,至少得傳一個,這是根據(jù)形參有幾個非半缺省參數(shù)確定的 TestFunc(1); TestFunc(1, 2); TestFunc(1, 2, 3); return 0; }
//a.hvoid TestFunc(int a = 10);//a.cppvoid TestFunc(int a = 20){}
注意:
- 半缺省參數(shù)必須從右往左依次來給出,且不能間隔著給
- 缺省參數(shù)不能在函數(shù)聲明和定義中同時出現(xiàn)
- 缺省值必須是常量或者全局變量
- C 語言不支持缺省
總結(jié)
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
C++實(shí)踐Time類中的運(yùn)算符重載參考方法
今天小編就為大家分享一篇關(guān)于C++實(shí)踐Time類中的運(yùn)算符重載參考方法,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-02-02C語言用fun函數(shù)實(shí)現(xiàn)兩個數(shù)的交換方式
這篇文章主要介紹了C語言用fun函數(shù)實(shí)現(xiàn)兩個數(shù)的交換方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12完全掌握C++編程中構(gòu)造函數(shù)使用的超級學(xué)習(xí)教程
這篇文章主要介紹了C++中的構(gòu)造函數(shù),包括C++11標(biāo)準(zhǔn)中的新特性的介紹,十分推薦!需要的朋友可以參考下2016-01-01C++實(shí)現(xiàn)LeetCode(59.螺旋矩陣之二)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(59.螺旋矩陣之二),本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07