2. C + + Upgrade

Source: Internet
Author: User


1. Inline function

Define can define macro code snippets, but C + + recommends using inline functions instead of macro snippets.

int f (intint  b) {}

You only need to add the inline keyword before the function definition (implementation). An inline function appears to replace a fragment of the macro code

Defined:

inline functions, in fact, will be inserted inside the internal function directly into the called place, so there is no, call the sub-function of the operation of the stack of the cost, to a certain extent, save the overhead.

Advantages:

Eliminate the call process, without the operation of the stack, reduce overhead, speed up the operation.

Disadvantages:

With an inline function, you insert a function so that the volume of the program becomes large

Note: in fact, it is to take the space overhead and time overhead to replace it, the inline function sacrifices the space overhead to save time overhead.

The difference between inline functions and macro code Snippets

An inline function is essentially a function that has the characteristics of a common post function, such as checking the parameters, returning types, and so on, inline functions are processed by the compiler, and the code is inserted directly into the calling place.

The macro code side is performed by preprocessing, just doing simple text substitution,

Limitations of inline functions

The advantage of inline functions is that the operation of the stack is reduced, and the overhead is saved, but when the inline function runs at a cost greater than the cost of the stack, the inline function is a chicken. There is also a limit to inline functions:

(1) There cannot be any form of circular statement

(2) cannot exist judgment statement

(3) The overhead of running overhead is greater than the cost of entering and exiting the stack, the average line of code does not exceed

(4) operation that cannot be taken to a function

The mechanism of the implementation of the inline function

When the compiler discovers an inline function, it

When the inline function is found, the type check is performed first, and the value inside the symbol table is inserted into the location of the call.

2, the function default parameters

C + + can provide a default value for the parameter when the function is declared , and when the function is used without specifying the value of the parameter, the compiler automatically uses the default value;

int  F (int1int1);  // Declaration of function, providing default value when declaring int  F (intint/ / function definition,{// ) Although no assignment is made to AB, the default value provided when using a function declaration     printf ("a =%d, b =%d \ n", A, b);}

function declarations provide rules for function default parameters

int  F (int int0int1int  f (intint b,int  c) {    printf ("a =%d, b =%d \ n", A, b);     return0;}

The rule is that once a function is provided with a default value from one start, then all default values must be provided, or an error is given, that is, if the example is provided with the default parameter value from the first A, then all subsequent arguments must provide a default parameter value.

When called, F (parameter), when the parameter is a numeric value, then this value will be passed to the variable a, when passed two, the value is passed to the variable A, B;

3, the function of the placeholder parameters

The placeholder parameter is the declaration of the type of the parameter of the function only, without the name of the parameter, for example:

int  F (intint b,int)

When used, we also have to pass in three parameters, but the last parameter is not used.

The meaning of the function placeholder parameter:

(1) Leave a clue for the future extension of the program: Tell the next program ape, this place can do the expansion,

(2) compatible with the C language program, the appearance of the non-standard wording,

4. Overloading of functions (overload)

Using the same function name to accomplish different functions, this is the overloaded function .

int  Func (int  a) {    returnint  func (int A,int b) {     return A +int  func (Char *s) {      return  strlen (s);}

As can be seen, the function names are exactly the same, but this function accomplishes different functions. So they must have differentiated, differentiated means: number of parameters, type of parameter, order of parameters


Problem: When overloaded functions meet a function to provide default parameters

int func (int A,int b,int0) {     return A + B +   int func (intint  b) {     return a + b;} int Main () {    func (12);  // which function to specify? }

Correctly, the compiler will error, the compiler found two semantics, two func can be executed, the compiler is also not understand,

Attention:

The return value of a function cannot be used as the basis for a function overload, that is, the overload of a function can only be distinguished by the number of arguments, the type of the parameter, and the order of the parameters.


5. C language and C + + mutual invocation


C + + calls a function written in the code:

C + + compiler is compatible with the C language compiler, but C + + compiler will prefer to use C + + compilation, but, if you want C + + calls C-language functions, you must add the extern keyword, tell the compiler, this function is compiled in C language way.

extern "c" {内容,比如函数或者头文件}

Tells the compiler that the part of the content, which can be a declared function or variable, uses the way the C compiler compiles

C calls a function written in C + +:


A unified solution:

__cplusplus:

So that C code can be compiled by the C compiler, or in C + + compiler, C, the way to compile,

#ifdef __cplusplus extern " C " {#endif//  function declaration or definition of function #ifdef __cplusplus} #endif

C + + compilers cannot compile function overloads in a way that compiles to the compiler

The overload of the function is only in C + +, so the problem of overloaded functions cannot be compiled in C, so,

#ifdef __cplusplusextern "C"{#endif //__cplusplus    intFuncintAintBintc =0)    {        returnA + B +C; }    intFuncintAintb) {returnA +b; } #ifdef __cplusplus}#endif

In the case of C-compiler function overloading, you will definitely get an error.

2. C + + Upgrade

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.