Function overloading of C + + Introduction (basic syntax)

Write in front

For those who don't understand the compilation and linking of C language, you can look at the following articles and review the previous knowledge first.
Explain the compilation and link of C language in detail

1 concept of function overloading

Function overloading: it is a special case of functions. C + + allows to declare several functions with the same name with similar functions in the same scope. The formal parameter list (number or type or order of parameters) of these functions with the same name must be different. It is commonly used to deal with the problems of similar functions and different data types.

//1. The number of parameters of the function is different
#include <iostream>
using namespace std;

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

int main()
{
	print();
	print(1);

	return 0;
}

The results are as follows:

print()
print(int a)
Please press any key to continue. . .
//2. The parameter types of functions are different
#include <iostream>
using namespace std;

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

int main()
{
	print(1.1);
	print(1);

	return 0;
}

The results are as follows:

print(double a)
print(int a)
Please press any key to continue. . .
//3. The parameter order of the function is different
#include <iostream>
using namespace std;

void print(double a, int b)
{
	cout << "print(double a, int b)" << endl;
}
void print(int b, double a)
{
	cout << "print(int b, double a)" << endl;
}
int main()
{
	print(1.1, 1);
	print(1, 1.1);

	return 0;
}

The results are as follows:

print(double a, int b)
print(int b, double a)
Please press any key to continue. . .

The above three cases support function overloading. Next, let's see whether the following two functions constitute function overloading?

int Add(int num1, int num2)
{
	return num1 + num2;
}

double Add(int num1,int num2)
{
	return num1 + num2;
}
int main()
{
	return 0;
}

Once we compile, the compiler will report the following errors:

Through the above analysis, we can find that whether to constitute a function overload is only related to the formal parameter list (number of parameters or type or order) of these functions with the same name, and has nothing to do with the type of return value of the function. Therefore, different return values cannot constitute function overloading and cannot be distinguished when calling.
Let's consider whether the following two functions constitute function overloading?

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

Once we compile, the compiler will report the following errors:

Therefore, different default values of function parameters do not constitute function overloading.

Finally, let's see whether the following two functions constitute function overloading?

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

Obviously, the above two functions constitute function overloading, and there is no problem in our compilation, but there will be problems when we call without passing parameters. For example, print() will have ambiguity when calling.

2 function overload principle

Through the above study, we now have a certain understanding and understanding of the syntax of function overloading. Then we will analyze the principle of function overloading with the following questions.

Why does C + + support function overloading, while C language does not support function overloading?

First, we create three files under Linux to verify the above problems, as follows:

//func.h
#include <stdio.h>

void f();
void f(int a);

//func.c
void f()
{
	printf("f()\n");
}
void f(int a)
{
	printf("f(int a)\n");
}

//test.c
#include "func.h"
int main()
{
	f();
	f(1);
	return 0;
}

Call the C compiler to compile test C and func C will report the following error:

Therefore, it is verified that C language does not support function overloading. Because when compiling, two overloaded functions have the same function name in func There are ambiguities and conflicts in the symbol table of O. Secondly, there are ambiguities and conflicts when linking, because they are identified and searched by function names, while overloaded functions have the same function names.
To verify the above statement, we mask a function and call the C compiler to compile test C and func c. An a.out executable program will be generated under linux. Use objdump -S to view this file:

Similarly, we cancel the function just shielded. Since C + + is compatible with C, we can compile the above program with the C + + compiler. The results are as follows

Use objdump -S to view a.out:

It is not difficult to see that the symbol table in the c + + object file does not directly use the function name to identify and find the function. Instead, the modification rules of function name are introduced. The modification rules of function name under different compilers are different.
Modification rules of function name in g + +_ Z + function name length + function name + parameter type initials.
With the modification rule of function name, as long as the function parameters are different, in func O the overloaded functions in the symbol table have no ambiguity and conflict.
Secondly, when linking, test When the main function in o calls two overloaded functions, it is also clear to look up the address in the symbol table.
This is the end of this article. If the content of this article is helpful to you, please triple praise, pay attention and collect support.
Creation is not easy, white whoring is not good. Your support and recognition is the biggest driving force of my creation. See you in the next article!
If there are any mistakes in this blog, please comment and advice. Thank you very much!

Keywords: C++ Back-end

Added by jmgrhm on Sun, 13 Feb 2022 09:40:10 +0200