Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use C++ default parameters

Shulou Source: shulou.com Published: 2022-06-02 03:40:50 10月22日 Update

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

Tags: Parameters functions code space two constructors caterpillars and at the same time data files very large the same that is representatives users content situations articles ways more Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Shulou Technology Huawei OPPO Reno vpn