Explanation of parameter default and function overloading

All the way through wind and rain to sharpen the will, three years of sharing joys and sorrows to create brilliance

Table of contents

1. The concept of parameter default

2. Default usage of parameters

3. Default parameter classification

3.1. All default parameters

3.2. Semi-default parameters

4. The concept of function overloading

5. Usage of function overloading

6. The principle of function overloading


1. The concept of parameter default

       In general, the number of actual parameters when calling a function should be the same as the number of formal parameters, but in order to use functions more conveniently, C++ also allows the definition of functions with default parameters . References are not the same.
       A default parameter refers to specifying a default value (default value) for a formal parameter when defining a function . When such a function is called, for the default parameter, the actual parameter value can be given, or no parameter value can be given. If the actual parameter is given, the actual parameter will be passed to the formal parameter for calling, if no actual parameter is given, the default value will be called.        Function call with default parameters: The default actual parameter does not have to be a constant expression, it can be any expression, and can even be given by a function call. If the default argument is any expression, the expression is re-evaluated each time the function is called. But the expression must make sense.
       

 


2. Default usage of parameters

Take the following picture as an example:

3. Default parameter classification

3.1. All default parameters

Full default means that all parameters are given default values

3.2. Semi-default parameters

Semi-default is not defaulting half of the parameters, but defaulting part of the parameters

Semi-default rule: must default from right to left , as in the following example:

  The calling method of a default parameter:

 The calling method of the default two parameters:

4. The concept of function overloading

Function focus means that in the same scope, there are multiple functions with the same name , but different formal parameter lists (different parameter types, different numbers of parameters, and different order of parameters) , and the return value is irrelevant. We call this overloaded function . Overloaded functions are distinguished by the parameter list, and have nothing to do with others. In a word, "one interface, multiple implementations".

For example: If you want to implement an addition operation, the addition function can be integer or floating point, you can use function overloading to achieve

5. Usage of function overloading

If you want to implement an addition operation, the addition function can be integer or floating point, you can use function overloading to achieve.

For example:

int Add(int a, int b)
{
	return a + b;
}
float Add(int a,float b)
{
	return a + b;
}
float Add(float a, int b)
{
	return a + b;
}
float Add(float a, float b)
{
	return a + b;
}

 5.1 Parameter types are different

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;
}

5.2 The number of parameters is different

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

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

5.3 The order of parameters 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 value is different and cannot constitute overloading, because it cannot be distinguished when calling

6. The principle of function overloading

(Preface: I don’t understand this part very deeply, so I don’t write it in such detail, let’s read it together~~)

Have you ever wondered why C language does not support function overloading, but C++ supports function overloading? How does C++ support it?

It is not convenient to demonstrate the principle of function overloading with the VS compiler, and the Linux operating system is required

Create a C language project under Linux system:

The declaration of the time function in func.h, the definition of the time function in func.c , and the main function in test.c

 We compile this project:

 At this point an error message will appear:

 Only by commenting out a f() function can the compilation succeed. The figure below is the execution result after commenting out the f() function without parameters:

 Here it is verified that the C language cannot support function overloading.

Compile and run the above code in C++:

 What is verified here is that C++ supports overloading.

Next, what we want to explore is why C language does not support function overloading, but C++ supports function overloading? How is C++ supported? Here comes the point (knock on the blackboard!!)

Let's review the process of compiling and linking:

 Among them, C language does not support function overloading and C++ supports function overloading is the reason for linking here.

Let's observe how the function call is implemented in the assembly code:

 C language does not support function overloading, because when compiling, the function names of the two overloaded functions are the same, there are ambiguities and conflicts in the Func.o symbol table, and there are ambiguities and conflicts when linking, because they are used directly The function name is used to identify and search, and the overloaded function has the same function name

C++ supports function overloading, because the function name is not directly used to identify and find functions in the symbol table of the C++ object file. C++ refers to a function name modification rule, but the modification rules are different under different compilers. With function name modification As a rule, as long as the parameters are different, there will be no ambiguity and conflict in the symbols of func.o. When linking, it is also clear to call two overloaded functions in the main function of test:o to find the address.

 The article ends here~~

 

Guess you like

Origin blog.csdn.net/m0_73648729/article/details/129472368