How to use C++ default parameters
This article will explain in detail how to use C++ default parameters, the quality of the article content is high, so Xiaobian share for everyone to make a reference, I hope you have a certain understanding of related knowledge after reading this article.
I. Default parameter concept
Default parameters specify a default value for a function's parameters when the function is declared or defined. When the function is called, the default value is taken if no arguments are specified, otherwise the specified arguments are used
#includeusing namespace std;void TestFunc(int a = 0)//Parameter default { cout 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;//you must write a triple eye here }}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(){ //assume you know exactly that you need at least 100 pieces of data here to st1 struct Stack st1; StackInitCpp1(&st1, 100); //Suppose you don't know how many data you need in st2---hope it's smaller struct Stack st2; StackInitCpp2 (&st1);//default return 0;} 2. Default parameter classification All default parameters void TestFunc(int a = 10, int b = 20, int c = 30){ cout