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

Default parameters and function overloading methods in C++

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

Share

Shulou(Shulou.com)05/31 Report--

Most people do not understand the knowledge points of this "C++ default parameters and function overloading method" article, so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article, let's take a look at this "C++ default parameters and function overloading method" article.

1. Default parameters

In C++, you can specify default values for parameters. When no argument corresponding to the formal parameter is specified when the function is called, the default parameter is automatically used.

Syntax and use of default parameters:

(1) assign a value to the parameter directly when the function is declared or defined. This is the default parameter.

(2) some or all of the parameters are omitted when the function is called. At this point, you can use default parameters instead.

Note:

(1) default parameters can only be set once in the function declaration. It can be set in the function definition only if there is no function declaration. (# add, which means when there are two parts of a function declaration and definition. Validation indicates that there is this limitation, which can be arbitrary, but for the sake of specification, it is specified in the declaration)

(2) if a parameter sets the default value, the parameter on the right side of the parameter must have a default value. (# add when this is defined, the parameter table of the member function of the class is declared on the right side of the parameter table by default. How to summarize when using it)

For example: int mal (int a, int bread3, int cantilever 6, int dumb8) is correct, set the default value from right to left.

Int mal (int aural 6, int bread3, int cantilever 5, int d) error, default value is not set from right to left. C sets the default value, and the d to the right has no default value.

(3) when the default parameter is called, the parameter call order is followed, one by one from left to right. This point should be clearly distinguished from paragraph (2) and should not be confused. (# add what does that mean? It is temporarily understood that the parameter between the two default parameters must be assigned a default value, which means that when the function is called, the first argument from left to right is the argument of the first parameter, and so on)

Such as:

Void mal (int a, int bread3, int cantilever 5); / / default parameter mal (3,8,9); / / if the parameter is specified when the call is made, the default parameter mal (3prime5) is not used; / / only two parameters are specified, which are called from left to right, which is equivalent to mal (3prime5); mal (3) / / only one parameter is specified, which is called in left-to-right order, which is equivalent to mal; mal (); / / error, because a has no default value mal (3, 9) / / error, so it should be called one by one from left to right, such as: void mal (int axioms 8, int banners 3, int citation 5); / / default parameter mal () / / correct, call all the default parameters, which are equivalent to mal (8jing3jin5)

(4) the default value can be global variable, global constant, or even a function. But it cannot be a local variable. Because the call to the default parameter is determined at compile time, the location of the local variable and the default value cannot be determined at compile time.

Second, function overloading

In the same declaration field, the function name is the same, but the parameter table is different. A special function usage that uniquely identifies and distinguishes a function by its parameter table.

The differences in the parameter table are as follows:

1. Different parameter types

2. The number of parameters is different

Special note: different return types cannot be used as an identification of function overloading.

Considerations for function overloading

The main results are as follows: 1. The formal parameters of the function must be different, or the number or type of the function must be different, so we can not only rely on the different return value types of the function or the different variable names of the formal parameters to realize the function overloading.

2. Do not define functions with different functions as overloaded functions, so as to avoid misunderstanding the result of the call. Such as:

Int add (int x pencil int y) {return x quoy;} float add (float x m m float y) {return x ray y;}

Ambiguity problems caused by overloaded functions overlapping with default parameters:

Func (int); / / overloaded function 1, with only one parameter and no default parameter

Func (int, int = 4); / / overloaded function 2, with 2 parameters and 1 default parameter

Func (int axiom 3, int baud 4, int censor 6); / / overloaded function 3, with 3 parameters and 3 default parameters

Fucn (float aversion 3.0, float baud 4.0 float clocks 5.0); / / overloaded function 4, with 3 parameters and 3 default parameters

Fucn (float aversion 3.0, float bounds 4.0 float clocks 5.0 float dumb7.9); / / overloaded function 5, with 4 parameters and 4 default parameters

Func (2); / / the first three functions can be called, resulting in ambiguity

Func (2.0); / / the last two functions can be called, resulting in ambiguity

So be aware of ambiguity when overloaded functions are used with default parameters.

The above is about the content of this article on "default parameters and function overloading methods in C++". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more related knowledge, please follow the industry information channel.

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