[C++] C++ entry function overloading

foreword

In natural language, a word can have multiple meanings, and people can judge the true meaning of the word through the context, that is, the word is overloaded.


1. Definition of function overloading

Function overloading : It is a special case of functions. C++ allows several functions with the same name to declare similar functions in the same scope . These functions with the same name have different formal parameter lists (number of parameters or types or order of types) , which are often used to deal with To achieve similar functions but different data types. For example:

#include<iostream>
using namespace std;

int Add(int left, int right)
{
    
    
	cout << "int Add(int left, int right)" << endl;
	return left + right;
}
double Add(double left, double right)
{
    
    
	cout << "double Add(double left, double right)" << endl;
	return left + right;
}
int main()
{
    
    
	int a = Add(1, 2);
	double b = Add(1.1, 2.2);
	cout << a << endl;
	cout << b << endl;

	return 0;
}

insert image description here
We can see that an Add function is defined to calculate the sum of two different types of numbers. During the calling process, the system will automatically realize the accurate call according to the different types of its parameters.

Not only functions can be overloaded in C++, operators can also be overloaded. For example:

Operators <<,>>. It can be used as a shift operator, as well as an output and input operator.

2. Conditions for function overloading

1. Different parameter types

int Add(int left, int right)
{
    
    
	cout << "int Add(int left, int right)" << endl;
	return left + right;
}
double Add(double left, double right)
{
    
    
	cout << "double Add(double left, double right)" << endl;
	return left + right;
}

2. The number of parameters is different

void f()
{
    
    
	cout << "f()" << endl;
}
void f(int a)
{
    
    
    cout << "f(int a)" << endl;
}

3. The order of parameter types is different

void f(int a, char b)
{
    
    
	cout << "f(int a,char b)" << endl;
}
void f(char b, int a)
{
    
    
	cout << "f(char b, int a)" << endl;
}

Note: The return types of function overloading can be the same or different, just different return types are not enough to be overloaded functions. Because calling a function does not necessarily need to receive the return value

3. The principle of function overloading – name mangling

Why does C++ support function overloading, but C language does not support function overloading?
In C/C++, for a program to run, it needs to go through the following stages: preprocessing, compiling, assembling, and linking.
insert image description here
During the compilation phase, the compiler will modify the function name

  • In C language,
    insert image description here
    because C language can only simply rename functions, the specific method is to add "_" before the function name; so adding two functions with the same name will have the same function name after compilation. Call Sometimes it will make an error because you don't know which one to call, so function overloading is not supported in C language.
  • In C++
    , in the compilation phase, although the function names of the two functions are the same, the compiler will modify their function names according to the conditions for forming function overloading , so that the names generated in the symbol table are different.
    Since the names in the symbol tables generated by the two functions are different in the .cpp file, it can be compiled.

The modification rules of C++ are different under different platforms. The modification rules of VS under Windows are somewhat complicated, while the modification rules of g++ under Linux are simple and easy to understand.

  • Under windows
    insert image description here
    ?Add@@YAHHH@Z
    ?Represents the start
    Add represents the original function name
    @@YA represents the start of the parameter
    The first H represents the return value type int
    The H after it represents the int parameter , M represents the float parameter
    @Z represents the name Finish.

  • under linux

insert image description here
_Z3Addii
_Z represents the beginning
3 represents the length of the original function name
Add represents the original function name
The first i represents the first parameter is int
The second i represents the second parameter is int

It can be seen that the function modification of g++ under Linux becomes [_Z+function length+function name+type initials].

4. Supplement and Summary

Polymorphism: use the same thing to represent different forms; polymorphism is divided into:

  • static polymorphism (compile-time polymorphism);
  • Dynamic polymorphism (polymorphism at runtime);

Function overloading is a kind of static polymorphism;

Overloaded functions are usually used to name a group of functions with similar functions in the same scope with the same function name. This reduces the number of function names, avoids the pollution of the namespace, and greatly improves the readability of the program. benefit.

Guess you like

Origin blog.csdn.net/qq_65207641/article/details/128853097