It has been a whole week since I last shared knowledge about C++, and I miss you all dearly! Now, let me briefly introduce today’s main topic: function overloading.
What is function overloading?
Function overloading is somewhat different from the concept of overloading in real life. Those who have a background in C may know that in C language, we are not allowed to have two functions with the same name. However, in C++, we can have functions with the same name. This is a special case of functions that allows us to declare several similarly functioning functions with the same name in the same scope. The parameter lists of these functions (the number or type or order of parameters) must be different, which is commonly used to handle the issue of different data types with similar functionalities.
Function overloading is an improvement made in C++ based on C language, solving the problem where functions with the same name cannot serve different types of parameters in C. For example, in C, to implement integer addition and floating-point addition, we cannot use the same name for the add function; we can only give these two functions different names, such as addi and addd, where i and d represent int and double, respectively.
Moreover, function overloading also enables operator overloading. The key points of using function overloading are as follows:
First, only functions with the same name in the same scope can be overloaded:
int add(int a, int b) // Global scope add { return a + b; }namespace myspace // myspace scope add { int add(int a, int b) { return a * b; } double add(double a, double b) { return a + b; }}
Among them, the global scope function int add(int int) and the myspace scope functions int add(int int) and double add(double double) do not constitute function overloading, while the functions int add(int int) and double add(double double) within the myspace scope belong to the same scope and have the same name, thus constituting function overloading.
Secondly, after meeting the basic requirement of having the same name in the same scope, function overloading can be divided into three types:
First type: functions with the same name but different parameter types:
int add(int a, int b) // Parameter type is int { return a + b; }double add(double a, double b) // Parameter type is double { return a + b; }
Second type: functions with the same name but different numbers of parameters.
double add(double a, double b) // Number of parameters is 2 { return a + b; }double add(double a, double b, double c) // Number of parameters is 3 { return a + b; }
Third type: functions with the same name but different parameter orders.
void add(int a, char b) { // do something }void add(char a, int b) { // do something }
It is worth noting that different return values do not constitute function overloading. For example, in the following two functions, the compiler cannot distinguish which function should be called based solely on the return value, resulting in an error: “Cannot overload functions distinguished only by return type”.
short add(short a, short b) { return a + b; }int add(short a, short b) { return a + b; }
Well, that concludes today’s sharing. If you have any related questions, feel free to message our backend, and I will be available 24/7 to answer your queries. More knowledge points will be shared in the future!