Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use C++ default parameters

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report