Summary of the main points of C + +--function

Source: Internet
Author: User
One inline inline function

Why does C + + have inline functions, what are inline functions, and what are the values of inline functions?

We already know that for a constant we can use const instead of a macro definition, such as a const int a=3; #define a 3

So for a function, can you also achieve the effect of such a macro substitution.

Please see:

#define MYFUNC (A, B) ((a) < (b)? (a):(B))//macro substitution represents the inline int myfunc (int a, int b)//function means {Returna < b? A:b;}

This is the inline function. Note the instructions

Illustration 1:

must be inline int myfunc (int a, int b) and the implementation of the function body, written in a piece

Description 2

The C + + compiler can compile a function inline

A function compiled inline by the C + + compiler is called an inline function.

Inline functions are not defined in the final generated code

The C + + compiler directly inserts the function body where the function is called

The additional overhead of an inline function without a normal function call (stack, jump, return)--existence value

Description 3:c++ compiler does not necessarily allow inline requests for functions!

Description 4

An inline function is a special function that has the characteristics of a common function (parameter checking, return type, etc.)

An inline function is a request to the compiler, so the compiler may reject the request

Inline functions are handled by the compiler, inserting the compiled function body directly into the calling place

Macro code fragments are processed by the preprocessor for simple text substitution without any compilation process

Illustration 5:

The modern C + + compiler is capable of compiling optimizations, so some functions may be compiled inline even without inline declarations

In addition, some modern C + + compilers provide extended syntax to enforce inline functions

Example: __attribute__ ((always_inline)) attribute in g++

Illustration 6:

Limitations of inline compilation in C + +:

There cannot be any form of circular statement

There cannot be too many conditional judgment statements.

The function body cannot be too large

function cannot be taken to the address

The function inline declaration must precede the calling statement

The compiler's limitations on inline functions are not absolute, and the benefits of inline functions relative to normal functions simply omit the overhead of stack, jump, and return on function calls.

Therefore, inline is meaningless when the execution overhead of the function body is much larger than the stack, jump, and return overhead.

Conclusion:

1) The inline function directly inserts the function body into the function call where it is compiled

2) Inline is just a request, and the compiler does not necessarily allow such a request

3) inline functions eliminate the overhead of pressing stacks, jumps, and returns during normal function calls

Two function default parameters

In C + +, you can provide a default value for a parameter when the function is declared (you need to call me as I provide your value)

When a function call does not specify a value for this parameter, the compiler automatically replaces it with the default value

void Myprint (int x = 3) {printf ("x:%d", x);}

Rules for function default parameters

Default parameter values are available only for parameters that follow the parameters list

Once you start using the default parameter values in a function call, all parameters after this parameter must use the default parameter values

void Printab (int x = 3) {printf ("x:%d\n", x);}

In the default parameter rule, if the default parameter appears, then the right side must have default parameters

void Printabc (int a, int b, int x = 3, inty=4, int z = 5) {printf ("x:%d\n", x);} int main62 (int argc, char *argv[]) {printab (2);p rintab (); System ("pause"); return0;}

Three. Function-placeholder parameters (first pit)

The placeholder parameter has only the parameter type declaration, and no parameter name declaration

In general, a placeholder parameter cannot be used inside a function body

int func (int a, int b, int) {Returna + b;} int Main01 () {//func (1, 2);//Can I? Error printf ("func (3) =%d\n", func (1, 2, 3)); GetChar (); Return0;}

Four. Default parameters and placeholder parameters (combined)

You can use the placeholder parameters in conjunction with the default parameters. Meaning: Leave clues for future expansion of the program. Compatible with C language programs may appear in the non-standard wording

int Func2 (int a, int b, int = 0) {return a + B;} void Main () {//If the default parameter and the placeholder parameter are together, it can be called up Func2; Func2 (3); system ("pause");

Conclusion://If the default parameters and the placeholder parameters are together, they can be called.

Five. function overloading (emphasis)

1) function overloading concept

function overloading (functionoverload)

Define different functions with the same function name

Functions have different meanings when function names are paired with different parameters



2) Evaluation criteria for function overloading

A function overload satisfies at least one of the following conditions:

Different number of parameters

Different parameter types

Parameter order is different

3) function return value is not a criterion for function overloading

4) Guidelines for compiler calls to overloaded functions

All functions with the same name as candidates

Try to find a viable candidate function

Exact match arguments

Can match arguments by default parameters

To convert matching arguments by default type

Match failed

The final viable candidate function found is not unique, then two semantics, compilation failed.

Unable to match all candidates, function undefined, compilation failed.

5) Considerations for function overloading

Overloaded functions are inherently independent of each other's functions (static chaining)

The function types of overloaded functions are different

function return value cannot be used as the basis for function overloading

The function overloads are determined by the name of the functions and the list of parameters.

Six. combination of function overloading and function pointers

function overloading and function pointers

When a function pointer is assigned using the overloaded functions name

Selecting candidates that are consistent with the parameter list of a function pointer based on overloaded rules

The function types that match the candidate's function type and function pointer strictly

Syntax for function pointer basics

1 Declaring a function type

typedef void (MYTYPEFUNC) (int a,int b); Int

Mytypefunc *MYFUNCP = NULL; Define a function pointer this pointer points to the entry address of the function

Declaring a function pointer type

typedef void (*MYPTYPEFUNC) (int a,int b); Declares the data type of a pointer

Myptypefunc fp = NULL; A function pointer is defined by a function pointer type.

Define a function pointer variable

void (*MYVARPFUNC) (int a, int b);

//

I want to add!

The above is a summary of the main points of C + +--function of the content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • 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.