Unescaped pitfalls-default parameters of C ++ functions (redefinition of default parameters)

Source: Internet
Author: User

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?

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.