C ++: Notes overloaded function


1. parameter name

#include <iostream>
using namespace std;
class A
{
public:
    //形参是i
    int f(int i)
    {
        cout << "int f(int i)" << endl;
        return 0;
    }

    //形参是n
    int f(int n)
    {
        cout << "int f(int n)" << endl;
        return 0;
    }

    //形参无名字
    int f(int)
    {
        cout << "int f(int)" << endl;
        return 0;
    }

    //形参无名字,类型Int只是int的别名
    typedef int Int;
    int f(Int)
    {
        cout << "int f(Int)" << endl;
        return 0;
    }
};

int main()
{
    A a;
    int i = 1;
    a.f(i);
    return 0;
}

Runtime Error

  1. Parameter name only play the role of mnemonic, its parameter contents of the list has no effect.
  2. Example 4 functions are equivalent, can only use one in the program, otherwise repeat function declaration error.
int f(int i)
int f(int n)
int f(int)
int f(Int)

2. Return Type

#include <iostream>
using namespace std;
class A
{
public:
    //返回类型是int
    int f(int i)
    {
        cout << "int f(int i)" << endl;
        return 0;
    }

    //返回类型是const int
    const int f(int i)
    {
        cout << "const int f(int i)" << endl;
        return 0;
    }

    //返回类型是double
    double f(int i)
    {
        cout << "double f(int i)" << endl;
        return 0;
    }
};

int main()
{
    A a;
    int i = 1;
    const int ci = 2;
    double d = 3.14;
    a.f(i);
    a.f(ci);
    a.f(d);
    return 0;
}

Error prompt

Runtime Error

  1. Function of three elements: the return type, function name, parameter type.
  2. In addition to the two functions are not allowed to return all the other elements are the same type, i.e., can not distinguish between the two types of return functions.
  3. Example 3 functions are equivalent, can only use one in the program, otherwise repeat function declaration error.
int f(int i)
const int f(int i)
double f(int i)

3. The parameter itself is const

#include <iostream>
using namespace std;
class A
{
public:
    //形参类型是int
    int f1(int i)
    {
        cout << "int f(int i)" << endl;
        return 0;
    }
    //形参类型是const int
    int f1(const int i)
    {
        cout << "int f(const int i)" << endl;
        return 0;
    }

    //形参类型是int *
    int f2(int *i)
    {
        cout << "int f(int *i)" << endl;
        return 0;
    }
    //形参类型是int *const
    int f2(int *const i)
    {
        cout << "int f(int *const i)" << endl;
        return 0;
    }
};

int main()
{
    A a;
    int i = 1;
    const int ci = 2;
    int *p1 = &i;
    int *const p2 = &i;
    a.f1(i);
    a.f1(ci);
    a.f2(p1);
    a.f2(p2);
    return 0;
}

Runtime Error

  1. Const is a constant i.e. the top layer itself, i.e., the underlying object pointed const is a constant.
  2. When arguments initialization parameter, the parameter is ignored const top layer, i.e., the function can not be distinguished by judging whether the parameter is the top const.
  3. Example, there is no difference between the two f1, only a programming optionally, f2 is no difference between the two, only one optional.
int f1(int i)
int f1(const int i)
int f2(int *i)
int f2(int *const i)

4. The parameter is a pointer to a const

#include <iostream>
using namespace std;
class A
{
public:
    //形参类型是int*
    int f(int *i)
    {
        cout << "int f(int *i)" << endl;
        return 0;
    }
    //形参类型是const int *
    int f(const int *i)
    {
        cout << "int f(const int *i)" << endl;
        return 0;
    }

    //形参类型是int &
    int f(int &i)
    {
        cout << "int f(int &i)" << endl;
        return 0;
    }
    //形参类型是const int &
    int f(const int &i)
    {
        cout << "int f(const int &i)" << endl;
        return 0;
    }
};

int main()
{
    A a;
    int i = 1;
    const int ci = 2;
    int *p1 = &i;
    const int *p2 = &i;
    a.f(p1); //p1指向int,所以重载int f(int *i)
    a.f(p2); //p2指向const int,所以重载int f(const int *i)
    a.f(i);  //i是int,所以重载int f(int &i)
    a.f(ci); //ci是const int,所以重载int f(const int &i)
    return 0;
}

operation result

  1. If the parameter is a pointer or reference const, const bottom can not be ignored, it can be determined by selection to a const argument if overloaded functions.
  2. If the parameter is a pointer or reference to const, constants or functions take const argument. If the parameter is not a reference point or const, function only accepts const argument.
  3. Example 4 is different functions, different functions according to the determined overload argument object.
int f(int *i)
int f(const int *i)
int f(int &i)
int f(const int &i)

5. const member functions

#include <iostream>
using namespace std;
class A
{
public:
    //普通成员函数
    int f1(int i)
    {
        cout << "int f1(int i)" << endl;
        return 0;
    }
    //常量成员函数
    int f2(int i) const
    {
        cout << "int f2(int i) const" << endl;
        return 0;
    }

    //普通成员函数
    int f3(int i)
    {
        cout << "int f3(int i)" << endl;
        return 0;
    }
    //常量成员函数
    int f3(int i) const
    {
        cout << "int f3(int i) const" << endl;
        return 0;
    }
};

int main()
{
    int i = 1;
    A a;
    const A ca;
    //类的对象不是常量时,可以调用常量或非常量的成员函数
    a.f1(i);
    a.f2(i);
    //类的对象是常量时,只能调用常量成员函数
    // ca.f1(i); //错误
    ca.f2(i);
    cout << "---------------------------" << endl;

    //重载函数
    a.f3(i);
    ca.f3(i);

    return 0;
}

operation result

  1. Constant object, and its references and pointers can only call const member functions. Whether the area can be classified by the object of constant decision overloaded functions.
Published 77 original articles · won praise 25 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_34801642/article/details/104893515