This is the fifth day of my participation in the August More text Challenge. For details, see:August is more challenging

This article is about function overloading in C++

Preface:

Function overloading means that if several functions in the same scope have the same name but different parameter lists, we call this function overloading.

In simple terms, the function name is the same, but the arguments are different, regardless of the return value type; But the real detail is that the function signature is different

1, global function overload

Function overloading depends on the parameter list, as shown in the following example:

void AA(int x){}
void AA(int x,int y){}
Copy the code

In the example above, the two functions have different argument lists, that is, different function signatures, which are two different functions with the same name.

The details:

Detail 1: Function overloading is independent of the return value

The return value is not part of the function signature, so the system does not recognize the two functions and the compiler prompts an error

Function signature definition: Contains the information about a function, including the function name, parameter types, number of parameters, order, and its class and namespace.

Detail 2: Has nothing to do with the invisible parameter name

Example:

void AA(int x){}
void AA(int){}
Copy the code

The signature of both functions is the same function

The typedef override type cannot be overridden

Example:

typedef int int_32
void AA(int x){}
void AA(int_32 x){}
Copy the code

It’s the same function, it’s the same function, it looks like the parameters are different, but inT_32 is not a new type, it’s just an alias for int, it’s not fundamentally different, so there’s no overloading, okay

Detail 4: Overloading and const parameters

Top const does not affect the parameter objects passed in. A parameter that has a top const cannot be distinguished from a parameter that does not;

Example:

void AA(const int x){}
void AA(int x){}

void BB(int *x){}
void BB(int * const x){}
Copy the code

The compiler cannot distinguish between the two parameters because the declaration is the same, that is, the function signature is the same

Overloading of the underlying const and non-underlying const parameters can occur to distinguish functions by distinguishing whether they are constant objects or nonconstant objects

Example:

void AA(const int &x){}/ / reference
void AA(int &x){}// Constant reference

void BB(int *x){}/ / pointer
void BB(const int *x){}// A pointer to a constant
Copy the code

In the example above, the compiler can infer which function to call by looking at whether the argument is constant. Since const cannot be converted to any other type, we can only pass a const object (or a pointer to const) to a const parameter. All four of the above functions can pass nonconstant, but the compiler prefers the nonconstant version to pass nonconstant or nonconstant Pointers.

Detail 5: Placeholder parameters and default parameters

Example:

void pp(int x,int=0){/ / a
    printf("int\n");
}
void pp(int x,int y=0){/ / 2
    printf("y\n");
} 
void pp(int x){/ / three
    printf("x\n");
}
Copy the code

One and two cannot be overloaded (completely), one and two cannot be overloaded (when only one argument is called), and two and three cannot be overloaded (when only one argument is called).

2. Overloaded member functions

Member function overloading is almost indistinguishable from global functions, but there is one detail that global functions do not exist

Details: The const at the end of the function name is part of the function signature and counts as a function overload

Example:

#include <iostream>
using namespace std;
class A
{
private: 
	int a;
	int b;
public:
        A() {};void AA(int a,int b)/ / a
	{
                printf("V");
    }
	void AA(int a,int b) const/ / 2
	{
		a=1;
		b=1;
                printf("*"); }};int main(int  argc ,const char  **argv)
{
        const A C;
        A B;
        C.AA(1.2);
        B.AA(1.2);
		return 0;
}
Copy the code

Output: * v

A function is an input call to a function that can be a constant or a variable, and a function is a call to a function that can only be a constant object, but the compiler will automatically distinguish between the two, constant calls to two functions, variable calls to one function