A brief introduction to C++ basic learning function overloading

  • 2020-06-15 09:49:57
  • OfStack

preface

There are several functions that we use in normal code that have the same implementation, but some of the details are different. For example, the value of exchanging two Numbers includes (int, float,char,double). In C we use different function names to make the distinction.


void Swap1(int* a, int* b);
void Swap2(float* a, float* b);
void Swap3(char* a, char* b);
void Swap4(double* a, double* b);

We can see that this code is not beautiful and has brought a lot of inconvenience to the programmer. Thus, in C++, people proposed to define multiple functions with one function name, which is called function overloading.

Function overloading refers to several functions within a scope with the same name but different parameter lists. These functions perform similar operations, but accept different parameter types, and the compiler selects the corresponding function call based on the passed argument type. This article provides a brief introduction to function overloading in C++.

Defining overloaded functions

Suppose you have a function that calculates the area of a graph, which can be the area of a triangle, a circle, or a square. The name of the function is the same, but according to the graph type passed in to select different functions to calculate the area, the program listing is as follows:


#include <iostream>
using namespace std;
typedef struct Triangle// define 3 Angle structure 
{
 double high;// high 
 double baseLen;// The bottom side 
}Triangle;
typedef struct Circle // Define circular structure 
{
 double radius;// The radius of 
}Circle;
typedef struct Square// Define the square structure 
{
 double sideLen;// Side length 
}Square;
// function 1. To calculate 3 Angle area 
double calcArea(const Triangle&)
{
 cout<<"calcute triangle area"<<endl;
}
// function 2. Calculate the circular area 
double calcArea(const Circle&)
{
 cout<<"calcute circle area"<<endl;
 return 0;
}
// function 3 To calculate the 3 Angle area 
double calcArea(const Square&)
{
 cout<<"calcute square area"<<endl;
}
int main(void)
{
 Triangle triangle;
 Circle circle;
 Square square;
 calcArea(triangle);// Call a function 1
 calcArea(circle);// Call a function 2
 calcArea(square);// Call a function 3
 return 0;
}

As you can see, the three function names defined, calcArea, are all the same, with different parameter types. The corresponding function is called when the triangle, circle, and square types are passed in respectively.

The operation results are as follows:

[

calcute triangle area
calcute circle area
calcute square area

]

As you can see, the corresponding functions are called when the types Triangle, Circle, and Square are passed in respectively.

Why reload

Function overloading relieves the programmer of the burden of naming a program. The most common example is constructor overloading.


class Test
{
 public:
  Test(void); //  No argument constructor 
  Test(int a);// The constructor 
  Test(int a,int b);// Constructor for two integer arguments 
};

As you can see, the three constructors of class Test are all named Test. Without overloading, three different constructor names may be required to implement three constructors, which increases the burden on the consumer of the class to use different constructor names when the consumer passes in different parameters to construct the object. With function overloading, the user of the class can pass in different parameters using the same function name.

Of course, it is unwise to use function overloading simply to lighten the burden of naming a function without losing the information it originally had. We can overload functions whose operations are really very similar.

A condition that cannot be overloaded

In the following cases, it is not allowed to overload or illegal.

The main function cannot be overloaded

This is stated in the C++ 11 standard:

[

A program shall contain a global function called main, which is the designated start of the program....
This function shall not be overloaded.

]

If the main function is overloaded as an entry function for a user program, which entry should be loaded?

Only the return value is different

For example, the following two declarations have only different return values and the same function name and formal parameters:


double calcArea(const Square&);
int calcArea(const Square&); // Invalid, only the return value is different, not overloaded 
/* An error will be reported if the above statement appears simultaneously */

Imagine 1. Which of the above functions should be called when you pass in an argument of type Square instead of using the return value?

The parameter list looks different, but it's the same

For example, typedef is used to give Triangle an alias:


typedef Triangle MyTri;
double calcArea(const Triangle&);
double calcArea(const MyTri&);
/* An error will be reported if the above statement appears simultaneously */

The above case looks quite different, but they are not fundamentally different.

The parameter names are different

Such as:


double calcArea(const Circle &circle );// Parameter called circle
double calcArea(const Circle& cir);// Parameter called cir
double calcArea(const Circle& );// Ellipse parameter names 
/* An error will be reported if the above statement appears simultaneously */

The parameter names here are for illustration or memory purposes only, so for the above three declarations, the parameter names can be arbitrary, but will not affect the contents of the parameter list.

Only the top const difference

Such as:


double calcArea(const Circle);// function 1
double calcArea(Circle);// The function is repeatedly declared 1
/* An error will be reported if the above statement appears simultaneously */
double calcArea(Circle* const);// function 2
double calcArea(Circle*);// The function is repeatedly declared 2
/* An error will be reported if the above statement appears simultaneously */

It is important to note, however, that if the parameter is a pointer or a reference, function overloading can be achieved by distinguishing between a reference to a large object and a constant object or a nonconstant object. For example, function overloading can be implemented in the following cases:


double calcArea(const Circle&);// Applies to constant references 
double calcArea(Circle&);//
/* The above statement appears at the same time without error */
double calcArea(const Circle*);// Operates on a constant pointer 
double calcArea(Circle*);
/* The above statement appears at the same time without error */

conclusion

After defining an overloaded function, we need to call it with reasonable arguments. In most cases, it's easy to tell which function to call with the corresponding argument passed in, but sometimes it's not so easy. We'll see how to do function matching in a later article.

Let's make a summary of the previous content:

Function overloading can ease the programmer's naming burden, but it should not cost readability. The main function cannot be overridden. The number or type of parameters for overloaded functions may vary. You cannot overload a function with a return value. The C language has no function overloading.

conclusion


Related articles: