[C++ Elementary] Introduction to C++ - Default Parameters, Function Overloading

insert image description here

1. Default parameters

1.1 Definition

 The default parameter is to specify a default value for the parameter of the function when declaring or defining the function . When calling the function, if no actual parameter is specified, the default value of the formal parameter is adopted, otherwise the specified actual parameter is used.

//缺省参数
void fun(int a = 10)
{
    
    
	cout << a << endl;
}

int main()
{
    
    
	fun(2);//传参了,使用显式传递的值
	fun();//没有传参,使用缺省参数
}

insert image description here
 The above code gives a default value of 10 in funthe formal parameter part of the function, which means that the funparameter can be passed or not when calling the function. If the parameter is passed, the formal parameter awill use the explicitly passed value. If not When passing parameters, ause the default value of 10 for the formal parameters.

1.2 Classification of default parameters

  • All default parameters
void text(int a = 5, int b = 15, int c = 25)//全缺省,所有参数都给了缺省值
{
    
    
	cout << a << ' ';
	cout << b << ' ';
	cout << c << ' ';
	cout << endl;
}

int main()
{
    
    
	text(1, 2, 3);
	text(1, 2);
	text(1);
	text();
	return 0;
}

insert image description here
 For all default parameters, when passing parameters, the parameters are defaulted in order from left to right , and the default cannot be skipped , that is: let the first formal parameter and the third formal parameter be passed explicitly Value, but let the second parameter use the default value, this practice is not allowed. The above code is an example: when no parameter is passed, all formal parameters use the default value; when only one parameter is passed, this parameter will be assigned to the first formal parameter, and the latter two formal parameters use the default value ;When passing two parameters, the first actual parameter will be assigned to the first formal parameter, the second actual parameter will be assigned to the second formal parameter, and the last formal parameter will use the default value; when passing three parameters When , all formal parameters are the values ​​​​passed by actual parameters. Here it should be distinguished from parameter push. The order of parameter push into the stack frame is from right to left.

  • semi-default parameter
void text(int a, int b = 15, int c = 25)//半缺省,只有部分参数给了缺省值
{
    
    
	cout << a << ' ';
	cout << b << ' ';
	cout << c << ' ';
	cout << endl;
}

int main()
{
    
    
	text(1, 2, 3);
	text(1, 2);
	text(1);
	return 0;
}

Note:
 Semi-default parameters must be given default values ​​from right to left , that is, the first formal parameter and the second formal parameter are given default values, while the third formal parameter is not given a default value, this kind of The situation is not allowed. Nor can it be separated, that is, the third formal parameter and the first formal parameter are given a default value, but the second formal parameter is not given a default value. This situation is also not allowed.

1.3 Default parameters can only appear in function declarations

 In order to avoid inconsistencies, it is required that default parameters cannot appear in function declarations and definitions at the same time, and can only appear in function declarations . Why can't it just appear in the definition of the function?
 Because the header file will be expanded in the preprocessing stage, the general function declaration is placed in the header file. If the default value is only given in the definition of the function, after the header file is expanded, there is no default parameter in the declaration statement of the function. At this time, if there is no parameter passed when calling the function and you want to use its default value, Then there will be problems in the process of program compilation, namely: the declaration of the function does not give a default value, which means that parameters need to be passed explicitly, but no parameters are passed when calling the function.

2. Function overloading

 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. For example: Some people say that there are two sports in our country that you don’t need to watch or worry about. One is table tennis and the other is men's football. The former means "no one can win!", and the latter means "no one can win!" But everyone understands that the former means that no one can win us, while the latter means that we cannot win others.

2.1 Definition

 Function overloading is a special case of functions. C++ allows several functions of the same name with similar functions to be declared in the same scope. The function is similar to the problem of different data types.

2.2 Several situations that constitute overloading

  • The 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;
}
int main()
{
    
    
	cout << Add(1, 2) << endl;
	cout << Add(1.0, 2.0) << endl;
}

insert image description here
 The above code defines two Addfunctions with the same name, but their parameter types are different. The first two parameters are both inttypes, and the second two parameters are doubleboth types. When calling Addthe function, the compiler will The type of the actual parameter automatically determines which function to call. As for why the compiler can do this, I will talk about it later.

  • The number of parameters is different:
void fun()
{
    
    
	cout << "f()" << endl;
}
void fun(int a)
{
    
    
 cout << "f(int a)" << endl;
}
int main()
{
    
    
	fun();
	fun(1);
	return 0;
}

insert image description here

  • The parameter type order is different:
// 3、参数类型顺序不同
void Text(int a, char b)
{
    
    
	cout << "Text(int a,char b)" << endl;
}
void Text(char b, int a)
{
    
    
	cout << "Text(char b, int a)" << endl;
}

int main()
{
    
    
	Text(1, 'a');
	Text('a', 1);
	return 0;
}
  • With default parameters
void fun()
{
    
    
	cout << "f()" << endl;
}
void fun(int a = 10)
{
    
    
 cout << "f(int a)" << endl;
}
int main()
{
    
    
	//fun();//无参调用会出现歧义
	fun(1);//调用的是第二个
	return 0;
}

 According to the definition of function overloading, the two functions in the above code funconstitute function overloading, and the compilation can pass, because the first one has no parameters, and the second one has an integer parameter, which belongs to the number of parameters above. Condition. But funthere is a problem with the function: there will be ambiguity when calling without parameters, because for both funfunctions, no parameters can be passed.
Note:
 The type of the return value has nothing to do with whether the function constitutes overloading. That is: the same function name, the same formal parameter list shape, and different return values ​​will not constitute function overloading.
insert image description here

2.3 C++ supports the principle of function overloading

 As mentioned above, the compiler will automatically determine which function should be called according to the type of the parameter, so how does the compiler do it? Answering this question will involve some knowledge of compiling and linking. Students who have forgotten it can read my previous article: [C Language Advanced] Compiling and linking . In fact, the concept of a function signature
 is involved here . A function signature contains information about a function, including the function name, its parameter types, its class and namespace, and other information . Function signatures are used to identify different functions , just like signatures are used to identify different people, and the name of the function is only part of the function signature. For functions with the same function name and different parameter lists, when the compiler and linker process symbols, they use a certain name modification method (different compilers will be different), so that each function signature corresponds to a modified name . During the assembly process, the compiler will modify the names of functions and variables to form symbolic names, that is to say, the symbolic names used in the target files after compiling the C++ source code are the modified names of the corresponding functions and variables. Both the C++ compiler and linker use symbols to identify and process functions and variables, so for functions with different function signatures, even if the function names are the same, both the compiler and linker think they are different functions.  Since the modification rules under Windows are too complicated, and the modification rules of g++ under Linux are simple and easy to understand, so I will use g++ to demonstrate the modified names below. Take the following code as an example:

#include <stdio.h>                                                                                                                                                                                            
int Add(int left, int right)
{
    
    
	printf("int Add(int left, int right)\n");
	return left + right;
}

double Add(double left, double right)
{
    
    
	printf("double Add(double left, double right)\n");
	return left + right;
}
int main()
{
    
    
	Add(1,2);
	Add(1.0,2.0);
	return 0;
}

 First use g++ text.cpp -o text.outthis instruction to compile the above code to generate an executable program tetx.out, and then use objdump -S text.outthe instruction to view the modified name.
insert image description here
 Among them _Zis a fixed prefix; 3it indicates the length of the function name; Addit is the function name; iit is an abbreviation of int, two i means that both parameters are of type int, and dit is an abbreviation of double, and two d means that both parameters are of double type. C++ is distinguished by function modification rules. As long as the parameters are different, the modified names are different, and overloading is supported. Through the analysis, it can be found that the modified name does not contain any information related to the return value of the function, so it is also verified that the type of the return value mentioned above has nothing to do with whether the function constitutes overloading.
 Looking at the processing results of the C language compiler gcc,
insert image description here
 it can be seen that after gcc compilation, the modification of the function name has not changed. This is why the C language does not support function overloading, because there is no way to distinguish functions with the same name.


 Today's sharing is over here! If you think the article is not bad, you can support it three times in a row . Your support is the driving force for Chunren to move forward!
insert image description here

Guess you like

Origin blog.csdn.net/weixin_63115236/article/details/131468717