C++的缺省參數(shù)你了解嘛
一、缺省參數(shù)概念
缺省參數(shù)是聲明或定義函數(shù)時為函數(shù)的參數(shù)指定一個默認值。在調(diào)用該函數(shù)時,如果沒有指定實參則采用該默認值,否則使用指定的實參
#include<iostream>
using namespace std;
void TestFunc(int a = 0)//參數(shù)缺省值
{
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()
{
//假設(shè)明確知道這里至少需要100個數(shù)據(jù)到st1
struct Stack st1;
StackInitCpp1(&st1, 100);
//假設(shè)不知道st2里需要多少個數(shù)據(jù) ———— 希望開小點
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,注意它沒辦法實現(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ù)據(jù)的函數(shù)總結(jié)
這篇文章主要介紹了C語言編程中從密碼文件獲取數(shù)據(jù)的函數(shù)總結(jié),包括getpw()函數(shù)和getpwnam()函數(shù)以及getpwuid()函數(shù),需要的朋友可以參考下2015-08-08
VS2019使用Windows桌面應(yīng)用程序模塊創(chuàng)建Win32窗口
這篇文章主要介紹了VS2019使用Windows桌面應(yīng)用程序模塊創(chuàng)建Win32窗口,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04
C C++ 題解LeetCode2360圖中的最長環(huán)示例
這篇文章主要為大家介紹了C C++ 題解LeetCode2360圖中的最長環(huán)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-10-10

