[C + +] Extended Properties

Source: Internet
Author: User

inline function

function overloading

Placeholder parameters and default parameters

/*__________________________________________________________________ Background: Const constants in C + + can override the definition of macro constants, const int A = 3; #define A 3 The inline function in C + + can replace the macro code fragment inline function is processed by the compiler, directly inserts the compiled function body into the call where the macro code fragment is processed by the preprocessor, for simple text substitution, without any compilation process 1) inline functions are compiled straight Where the function body is inserted into the function call 2) inline is just a request, the compiler does not necessarily allow such a request 3) The inline function eliminates the need for a normal function call when the stack, jump and return the cost of the inline compilation limit in C + +: cannot exist any form of circular statement * cannot be saved In too many conditional judgment statements * Function body cannot be too large * cannot fetch function * Function Inline declaration must be *_____________________ before calling the statement _____________________________________________*/#include<iostream>using namespacestd;#defineMYFUNC (A, B) ((a) < (b)? (a):(B))inlineintMyFuncintAintb) {return(A < b?)a:b);}intMainvoid) {    intA =1, B =3; intc =0;//C = MYFUNC (1, 3);c = MyFunc (1,3); cout<<"C:"<< C <<Endl; return 0;}

/*———————————————————————————————————— 1 Function overloading conceptual function overloading (functions overload) defines different functions with the same function name when the function name and the different parameters match the meaning of the functions 2 function overloads The break standard function overload satisfies at least one of the following conditions: Number of parameters different parameter types different parameter order!!!    The function return value is not the criterion of the function overload 3. The compiler invokes the criteria for overloaded functions The exact match argument can match the argument through the default type transform match argument matching failure finally find the feasible candidate function is not unique, then two semantics, compilation failed. Unable to match all candidates, function undefined, compilation failed.        4. Overloaded functions are inherently independent of each other functions (static chaining) function overloading is determined by the functions name and the parameter list. function return value cannot be overloaded as a function type of the overloaded function is different from the function overload that occurs inside a Class 5. function overloading and function pointers when assigning a function pointer using overloaded functions names according to overloaded rules Select candidates that are consistent with the function pointer parameter list to strictly match the function type of the candidate and the function type of the functional pointer ————————————————————————————————————*/#include<iostream>#include<cstring>using namespacestd;intFuncintx) {returnx; }intFuncintAintb) {returnA +b; }//The function is ambiguous when the default parameter of the function is encountered, and the compilation fails//int func (int a, int b, int c = 0) {return a * b *c;} intFuncConst Char* s) {returnstrlen (s);}//function overloading and function pointerstypedefint(*pfunc) (inta);//int (int a)intMainintargcChar*argv[]) {    intc =0; C= Func (1); printf ("C =%d\n", c); C= Func (1,2); printf ("C =%d\n", c); C= Func ("12345"); printf ("C =%d\n", c); PFUNC P= func;//p is a function pointer variablec = P (1); printf ("C =%d\n", c); return 0;} 

/*____________________________________________________________________________________c++ can provide a default value for a parameter when the function is declared , when a function call does not specify a value for this parameter, the compiler automatically replaces the rule with the default value for the default parameter of the function once the default parameter value is started in a function call, all arguments after this parameter must use the default parameter value for the default declaration to attempt to change the default value function placeholder Parameter placeholder parameter only argument type declaration, without parameter name declaration in general, the placeholder parameter can be used within the body of the function to combine the placeholder parameters with the default parameters to use meaning for the extension of the program to leave clues compatible with the C language program may appear in the non-compliant Fan notation ____________________________________________________________________________________*/#include<iostream>using namespacestd;voidPrintfab (intx =3) {printf ("x:%d\n", x);} //in the default parameter rule, if the default parameter appears, then the right side must have default parametersvoidPRINTABC (intAintBintx =3,inty =4,intz =5) {printf ("x:%d\n", x);}intFuncintAintBint)  {//placeholder parameters placeholder Para    returnA +b;}/*=================== combined with ===================*/intPlace_default (intAintBint=0) {    returnA +b;}intMainintargcChar*argv[]) {Printfab (2);        Printfab (); printf ("func (1, 2, 3) =%d\n", Func (1,2,3));//can not func (1, 2);        intC1 =0, C2 =0; C1= Place_default (1,2);//OKC2 = Place_default (1,2,3);//OKcout <<"C1:"<< C1 <<Endl; cout<<"C2:"<< C2 <<Endl; return 0;}

[C + +] Extended Properties

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.