C + + basic inline default parameter function occupation parameter function overload

1. inline function

Inline functions are used to replace macros,

Example:

Among them, macro and + + have side effects.

#include "iostream"
using namespace std;
#define MYFUNC(a, b) ((a) < (b) ? (a) : (b))  

inline int myfunc(int a, int b) 
{
    return a < b ? a : b;
}

int main()
{
    int a = 1;
    int b = 3;
    //int c = myfunc(++a, b);  
    int c = MYFUNC(++a, b);  

    printf("a = %d\n", a); 
    printf("b = %d\n", b);
    printf("c = %d\n", c);

system("pause");
    return 0;
}

 

There are the following notes:

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

(2) the inline function is not defined in the final generated code. The C + + compiler directly inserts the function body into the function call, so the inline function does not have the extra cost (stack pressing, jump, return) and cannot take the address.

(3) the inline function is processed by the compiler, which directly inserts the compiled function body into the calling place. The macro is processed by the preprocessor, which makes simple text replacement without any compilation process.

(4) restrictions on inline compilation:

There cannot be circular statements, too many judgment statements, too large function body, and address of function body. Inline declaration must be prior to the call statement.

2. Default parameters

As follows:

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

Rules:

(1) only the later part of the parameter list can use the default parameter.

(2) once the default parameter is used in the function, the parameters after this parameter must use the default parameter.

//In the default parameter rule, if the default parameter appears, all the parameters on the right must have default parameters
void printABC(int a, int b, int x = 3, int y=4, int z = 5)
{
    printf("x:%d\n", x);
}
int main62(int argc, char *argv[])
{
    printAB(2);
    printAB();
    system("pause");
    return 0;
}

3. Function occupation parameter

As follows:

int func(int a, int b, int ) 
{
    return a + b;
}

int main01()
{
    //func(1, 2); //OK?
    printf("func(1, 2, 3) = %d\n", func(1, 2, 3));

    getchar();    
    return 0;
}

Function occupation parameter:

(1) the placeholder parameter has only parameter type declaration, but no parameter name declaration

(2) in general, the occupation parameter cannot be used inside the function body.

 

Function occupation parameter combined with default parameter

int func2(int a, int b, int = 0)
{
    return a + b;
}
void main()
{
    //If the default parameter and occupation parameter are together, they can be called
    func2(1, 2);
    func2(1, 2, 3);
    system("pause");
}

4. Function overload

(1) the concept of function overload

When the function name or parameters are different, the meaning of the function is different.

(2) judgment standard of function overload

Parameter type, number and order

The return value of is not a criterion.

(3) the criterion of compiler calling function overload

Use all functions with the same name as candidates

Precisely match parameters, and match parameters with default parameters

If it fails, perform the default type conversion matching argument.

If it fails, it fails.

Ambiguity is not allowed for a match.

(4) function overload and function default parameters

//What happens when a function's default parameter is overloaded

int func(int a, int b, int c = 0)
{
    return a * b * c;
}

int func(int a, int b)
{
    return a + b;
}

//1 Are parameters allowed
int func(int a)
{
    return a + b;
}


int main()
{
    int c = 0;

    c = func(1, 2); // Ambiguous, call failed, compilation failed 

    printf("c = %d\n", c);

    printf("Press enter to continue ...");
    getchar();    
    return 0;
}

5. Function overload and function pointer

/*
Function overloading and function pointer
    When a function pointer is assigned with an overloaded function name
    Select the candidate consistent with the function pointer parameter list according to the overload rule
    Strictly match the function types of candidates and function pointers
*/

int func(int x) // int(int a)
{
    return x;
}

int func(int a, int b)
{
    return a + b;
}

int func(const char* s)
{
    return strlen(s);
}

typedef int(*PFUNC)(int a); // int(int a)

int main()
{
    int c = 0;
    PFUNC p = func;

    c = p(1);

    printf("c = %d\n", c);

    printf("Press enter to continue ...");
    getchar();    
    return 0;
}

Keywords: C++

Added by Leadwing on Sun, 15 Dec 2019 20:25:06 +0200