Unescaped pitfalls-default parameters of C ++ functions (redefinition of default parameters)
The default parameter indicates that a value is automatically used when real parameters are omitted in a function call.
Note the following:
For a function with a parameter list, the default value must be added from right to left.
That is to say, to set the default value for a parameter, you must provide the default value for all parameters on its right.
What we encounter today is that the function uses default parameters, and the Declaration and definition of the function are separated.
Char * left (const char * str, int n = 1); int main () {}char * left (const char * str, int n = 1) // error {}
The above code can be modified in two ways:
1. the parameter is declared with a default parameter.
char* left(const char* str, int n=1);int main(){}char* left(const char* str, int n ){}
2. There are default parameters in implementation, and no default value in Declaration.
char* left(const char* str, int n);int main(){}char* left(const char* str, int n = 1){}
Another mistake we often make.
In many cases, we can use default parameters to replace function overloading:
void point(int,int){//...}void point(int a){return point(a,4);}void point(){return point(3,4);}
The following default parameter functions can be used as an alternative:
void point(int=3,int=4);
When "point ();" is called, that is, "point (3, 4);" is called, which is 3rd declared overload functions.
When "point (6);" is called, that is, "point (6, 4);" is called, which is 2nd declared overload functions.
When "point (1st);" is called, declared overload functions are called.
If a group of overload functions (which may contain default parameters) allow calls with the same number of instances, this will lead to the ambiguity of calls.
Void func (int); // void func (int, int = 4), one of the reload functions; // reload function 2, with the default parameter void func (int = 3, int = 4); // The third overload function with the default parameter func (7); // error: Which of the three overload functions is called? Func () // error: Which of the following two overload functions is called?