8.3 Default values for parameters
It is annoying to write a statement that has the same value for some parameters every time the function is called. The C + + language uses the default value of the parameter to make writing concise (the default value is automatically inserted by the compiler at compile time).
Rule of Use for parameter defaults:
The l "rule 8-3-1" parameter default value can only appear in the declaration of a function, not in the definition body.
For example:
void Foo (int x=0, int y=0); Correct, default values appear in the declaration of the function
void Foo (int x=0, int y=0)//error, the default value appears in the function's definition body
{
...
}
Why is that? I think there are two reasons: one is that the implementation (definition) of a function has nothing to do with the default value of the parameter, so it is not necessary to have the default value appear in the function's definition body. The second is that the default value of the parameter may change, and it is obvious that modifying the function's declaration is easier than modifying the function's definition.
L "Rule 8-3-2" if a function has more than one argument, the argument can only be asked from the backward forward, or it will cause the function call statement to be absurd.
The correct example is as follows:
void Foo (int x, int y=0, int z=0);
Examples of errors are as follows:
void Foo (int x=0, int y, int z=0);
Note that using the default value of the parameter does not give the function a new function, just to make the writing concise. It may improve the ease of use of the function, but it may also degrade the function's understandable nature. So we can only use the default values of parameters appropriately, to prevent improper use of negative effects. In the example 8-3-2, the unreasonable use of the default value of the parameter will result in two semantics of the overloaded function output.
#include <iostream.h>
void output (int x);
void output (int x, float y=0.0);
void output (int x)
{
cout << "Output int" << x << Endl;
}
void output (int x, float y)
{
cout << "Output int" << x << "and float" << y << Endl;
}
void Main (void)
{
int x=1;
float y=0.5;
Output (x); error! Ambiguous call
Output (x,y); Output int 1 and float 0.5
}
The default value of the example 8-3-2 parameter causes the overloaded function to produce ambiguity
8.4 Operator Overloading
8.4.1 Concept
In the C + + language, you can use the keyword operator plus operator to represent a function, called an operator overload. For example, two complex number addition functions:
Complex Add (const Complex &a, const Complex &b);
You can use operator overloading to represent:
Complex operator + (const Complex &a, const Complex &b);
The difference between an operator and a normal function when invoked is: for a normal function, the argument appears in parentheses, and for the operator, the argument appears on its left and right. For example
Complex A, B, C;
...
c = Add (A, b); Using Ordinary functions
c = a + B; With operator +
If the operator is overloaded as a global function, then the operator with only one parameter is called a unary operator, and an operator with two arguments is called a two-dollar operator.
If the operator is overloaded as a member function of a class, then the unary operator has no arguments, and the two-dollar operator has only one right argument because the object itself is the left argument.
Syntactically, an operator can be defined either as a global function or as a member function. The literature [Murray, p44-p47] has made a lot of elaboration on this issue, and summarized the rules of table 8-4-1.
Operator
Rules
All unary operators
Recommended overload as member function
= () []->
can only be overloaded as member functions
+ = =/= *= &= |= ~=%= >>= <<=
Recommended overload as member function
All other operators
Recommended overloads as global functions
Overloading rules for table 8-4-1 operators