c ++ - overloaded inline function and functions and function pointers and default parameters

Inline functions

C ++ inline functions are commonly used with classes. If a function is inline, then at compile time, the compiler will copy the code for the function of each placed where the function is called.

Any modification inline function, we need to recompile the function of all clients, because the compiler need to be replaced once all the code, otherwise it will continue to use the old function.

If you want a function is defined as an inline function, you need to place the keyword inline in front of the function name, need to be defined before calling the function. If the function has been defined more than one line, the compiler will ignore the inline qualifier.

Functions defined in the class definition are inline functions, even without using inline specifier.

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>


using namespace std;

#define MAX(a, b) \
    ((a)>(b)?(a):(b))

int max(int a, int b)
{
    return (a > b) ? a : b;
}

inline void printAB(int a, int b);


int main(void)
{
    int a = 10;
    int b = 20;
    int c = 0;

    // MAX(a++, b++);
    
    cout <<"c = " <<c<<endl;
#if 1
    for (int i = 0; i < 1000; i++) {
        a++;
        b++;
        printAB(a++, b++);
    }
#endif
    return 0;
}

inline void printAB(int a, int b)
{
    cout << "a = " << a << ", b = " << b << endl;
}

Default parameters (parameter has a default value, there is a default parameter, the default parameters are the right ones)

And positional parameters with default int func (int a, int b, int = 0)

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>


using namespace std;

void func(int a = 666)
{
    cout << "a = " << a << endl;
}

//求立方体体积
int get_volume(int len, int width=199, int height=10)
{
    cout << "len = " << len << endl;
    cout << "w = " << width << endl;
    cout << "h = " << height << endl;

    return len *width*height;
}


void func2(int x, int=0)//亚元
{
    cout << "x =" << x << endl;

}

int main(void)
{
    int value = 10;

    func();

    int len = 10;
    int w = 20;
    int h = 30;


    cout << "体积是" << get_volume(w,h) << endl;

    func2(199, 10);
    func2(200);

    return 0;
}

Function overloading

Placeholder function parameter operator overloading rear ++ int func (int a, int b, int) can not use the functions that parameter placeholder

In the same scope, similar function with the same name can declare several functions, but these functions with the same name in the form of parameters (parameter refers to the number, type or order) must be different. You can not only by overloaded functions return different types.

  • Concept (function names as function parameters are not the same)
  • Analyzing the function return value is not standard
  • Call rules (by name, number, type)
  • Event of default function arguments function overloading, the call is ambiguous
  • Function pointer syntax: type defined function defined function pointer type definition function pointer variables
  • With function pointers and function overloading
    #define _CRT_SECURE_NO_WARNINGS
#include <iostream>


using namespace std;

//函数的返回值, 函数形参列表(参数的个数,参数类型,参数顺序)
//函数重载  函数名相同, 参数列表不同
//函数返回值并不是构成函数重载的条件
int func(int a, int b) 
{
    cout << "func1" << endl;
    return 0;
}

//如果要是函数重载话,不要写默认参数,为了避免调用出现函数冲突
char func(int a, int b, int)
{
    cout << "func2" << endl;
    return 0;
}

#if 1
int func(int a, char *str)
{
    cout << "func3" << endl;
    return 0;
}
#endif

void print1(int a)
{
    cout << "print 1" << endl;
    cout << "a = " << a << endl;
}

void print1(double b)
{
    cout << "print 2" << endl;
    cout << "b = " << b << endl;
}
void print1(char ch)
{
    cout << "print 3" << endl;
    cout << "ch =" << ch << endl;
}

int main(void)
{
    func(10, 20);

    func(10, "abc");

    print1(10);

    print1(19.00);

    print1(3.1f);

    print1('a');//char->int

    //print1("itcast");
    //1 如果能够严格匹配调用完全匹配的
    //2 如果没有完全匹配,调用隐士转换
    //3 都匹配不到,调用失败。

    return 0;
}

Function pointer and function overloading

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>

using namespace std;



int func(int a, int b)
{
    cout << "func(int, int)" << endl;
    return 0;
}

int func(int a, int b, int c)
{
    cout << "func(int, int,int )" << endl;
    return 0;
}

//1 . 定义一种函数类型
typedef int(MY_FUNC)(int, int);

//2 顶一个指向之中函数类型的指针类型
typedef int(*MY_FUNC_P)(int, int);

int main(void)
{
    //1.
    MY_FUNC *fp = NULL;
    fp = func;
    fp(10, 20);


    //2.
    MY_FUNC_P fp1 = NULL;

    fp1 = func;
    fp1(10, 20);

    //3. 
    int(*fp3)(int, int) = NULL;
    fp3 = func;
    fp3(10, 20);

    func(10, 20);
    func(10, 20, 30);

    fp3 = func; //fp3 ---> func(int,int)

    //实际上在给函数指针赋值的时候,是会发生函数重载匹配的
    //在调用函数指针的时候,所调用的函数就已经固定了。
    int(*fp4)(int, int, int) = NULL;
    fp4 = func; //fp4 ---> func(int,int,int)


    fp3(10, 30);//func(int,int)
    fp3(10, 20);

    fp4(10, 30, 30);

    return 0;
}

Guess you like

Origin www.cnblogs.com/ygjzs/p/12074412.html