Default parameter learning for functions in C + +

Source: Internet
Author: User

    The default parameter refers to the automatic use of a default value when an argument is omitted from a function call.

For a function with a parameter list, you must add a default value from right to left (that is, her right side must have a default value first)

Fun (int, int,1,int,int), etc. It's clear enough!!

1 Char* Left (Const Char* STR,intn=1);2 intMain ()3 {4 }5 6 Char* Left (Const Char* STR,intn =1)//Error7 {8}

1 declaration with default parameters, no default value when implemented (OK)

2 implemented with default parameters, no default value declared (OK)

1 Char* Left (Const Char* STR,intn=1);2 intMain ()3 {4 }5 6 Char* Left (Const Char* STR,intN)7 {8 9}
1 Char* Left (Const Char* STR,intn);2 intMain ()3 {4 5 }6 Char* Left (Const Char* STR,intn =1)7 {8 9}

Use default parameters to override function overloading:

If a set of overloaded functions (which may have default parameters) allows the same number of calls , the two semantics of the call will be raised.

1 voidFuncint);//one of the overloaded functions2 voidFuncint,int=4);//two of the overloaded functions with default parameters3 voidFuncint=3,int=4);//three of the overloaded functions with default parameters4 5Func7);//error: Which of the 3 overloaded functions is called?6Func -, -)//error: Which one of the following 2 overloaded functions is called?

Careful,!!!!!!!!!!!!!.

Default parameter learning for functions in C + +

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.