Document directory
- 2. The default value of the parameter can only be specified in the declaration or definition. It cannot be specified at the same time.
- 3. ambiguity between default parameters and function overloading.
The default parameters are very useful when there are many function parameters. You can pass only the required values. For others, use the default values as follows:
1. The default parameters are used strictly from left to right.
Therefore, only the following operations are valid:
(1) All parameters are default values. Void fun (int A = 1, int B = 2, int c = 3)
(2) void fun (int A, int B = 2, int c = 3)
(3) void fun (int A, int B = 2, int c) // This is incorrect. If the default value is used from somewhere, all other parameters on the right must also have default values.
When a function is called, the number of passed arguments must be greater than or equal to the number of parameters without the default value. Then, the parameter is matched from left to right. However, this may cause some problems.
For example
Void fun (INT one, int two = 2, int three = 3 );
If you want to pass only the values of one and three provided by the two parameters during the call, it is impossible because the values must be strictly matched from left to right, you must also pass values to all parameters before it.
Some people naturally think of this problem, so we suggest that stroustrup, the father of C ++, add a feature that can explicitly assign values to the first few parameters. for example, you can call the above function in this way fun (one = 1, three = 3), or even not in order.
Fun (three = 3, one = 1) so that users can randomly specify the parameter to be assigned. Of course, parameters without default values must be assigned.
Function calls in PL/SQL fully reflect this idea.
However, stroustrup thinks that the benefits brought by this operation are not too great and there are some drawbacks. Therefore, it is not added to C ++. the drawback is that the function declaration and the name of the parameter in the definition can be different. in this way, it is more difficult to explicitly specify the passed real parameters by the name of the form parameter. for example, the declaration is void fun (INT one, int two), but the definition is changed to void fun (INT two, int one ){}
2. The default value of the parameter can only be specified in the declaration or definition. It cannot be specified at the same time.
For example, there is a class Arwen, and then declare the function fundefault in the header file, and then define it in the CPP file. The following two methods can be used:
Method (1)
Int fundefault (INT one, int two = 123 );
// Statement
Int Arwen: fundefault (INT one, int two) // define
{
//....
}
Method (2)
Int fundefault (INT one, int two );
Int Arwen: fundefault (INT one, int two = 123)
{
//....
}
But the following is incorrect.
Method (3)
Int fundefault (INT one, int two = 123 );
Int Arwen: fundefault (INT one, int two = 123)
{
//....
}
In fact, the method (3) is the most intuitive and easy to understand. however, it may be a little troublesome to repeat the assignment in both places. Secondly, the compiler must make judgments to ensure that the two default values are consistent, so we simply won't let this happen.
3. ambiguity between default parameters and function overloading.
Assume there is a function
Void fun (INT one, int two = 2 );
Void fun (INT one );
In this call, fun (1); the above two functions are completely matched, so there is ambiguity. An error will be reported during compilation.
However, it is a bit strange that an error is reported only when you have both function declarations and definitions. if the Declaration and definition are both in the header file, that is, the inline function (Inline), no error will be reported if the preceding statement is used. fun (1) calls void fun (INT one, int two = 2). I tried it in Vs and I don't know if other compilers have done the same processing.
Of course, we 'd better avoid this situation in the code. in fact, the default parameter is used to replace the function overload (just like the two functions above, when a function only has more parameters than another function, other parameters are the same ). therefore, it is unnecessary to have such default parameters and overload functions in well-designed code.