C + + default parameters

Source: Internet
Author: User

In C + +, you can specify a default value for the parameter. When a function call does not specify an argument that corresponds to a formal parameter, the default parameter is automatically used.

The default parameter syntax is used with:
(1) when a function is declared or defined, the parameter is directly assigned a value. This is the default parameter ;
(2) Omit some or all of the arguments when the function is called. You can use default parameters instead.

Note:
(1)The default parameter 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, this sentence means that there are two parts of a function declaration and definition.) The validation indicates that there is this restriction, which can be casually, but is specified in the Declaration for specification.
(2)If a parameter is set to a default value, itsthe parameters on the right must have default values. (#add this is defined,The parameter table for the member function of the classStatementWhen the default parameter is in the parameterThe right side of the tables, how to summarize when using)
such as: int mal (int a, int b=3, int c=6, int d=8) are correct, set default values in right-to-left order.
int mal (int a=6, int b=3, int c=5, int d) error, default value is not set from right to left. C Sets the default value, and the D on the right has no default value.
(3)when the default arguments are called, they are invoked sequentially from left to right, following the argument invocation order. This point should be clearly divided with paragraph (2) and not be confused. (#add What does God mean? The parameter between the two default parameters must be assigned a default value, wrong,When a 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 b=3, int c=5); Default parameters
Mal (3, 8, 9); When called with a specified argument, no default parameter is used
Mal (3, 5); Only two parameters are specified on invocation, in order from left to right, which is equivalent to Mal (3,5,5);
Mal (3); Only 1 parameters are specified on invocation, in order from left to right, which is equivalent to Mal (5,3,5);
Mal (); Error because a does not have a default value
Mal (3,, 9)//error, should be called one by one in order from left to right
Again such as: void Mal (int a=8, int b=3, int c=5); Default parameters
Mal (); Correct, call all default parameters, equivalent to Mal (8,3,5);

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

The most typical is the constructor with default parameters:

For example, you define a Class A;   A has two private data members int A; and int b; If you do not use the default constructor, the code is as follows:

A (); Constructors with no parameters

A (int a,int b);//constructor with parameters

If you are creating a new object, such as initializing

A A1; Initialize call a ();

A A2 (3,4); Assignment calls a (int a,int b);


If you use a constructor with a default parameter, you do not need a constructor without parameters (using a constructor with default parameters is equivalent to writing the two constructors above ), as follows:

A (int a=0,int b=0);


You do not need to write a constructor without parameters, because if you define an object like this:

A A1; called a (Inta=0,int b=0) because the compiler no longer produces the default constructor a (), but instead calls a (int a=0,int b=0) directly

Because a constructor with no arguments is the default constructor, the constructor that specifies the default value for the parameter is also the default constructor, and a class can have only one default constructor

the point here is that a class does not automatically generate a default constructor after writing a default constructor, and if you write a constructor with default arguments, you already have a default constructor

C + + default parameters

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.