C++ - default parameters and function overloading

Table of contents

default parameters

concept

Types of default parameters:

All default parameters

semi-default parameter

function overloading

concept

The number of parameters is different

Different kinds of parameters

The order of the parameter types is different

The underlying principle - name mangling


default parameters

concept

The default parameter is to specify a default value for the parameter of the function when the function is defined or declared. When the function is called, if the actual parameter of the function is not specified, the default value of the formal parameter is used.

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

int main()
{
	Func();//打印结果是0
	Func(4);//打印结果是4
	return 0;
}

Types of default parameters:

  • All default parameters

void Func(int a = 10, int b = 20, int c = 30)
{
    cout<<"a = "<<a<<endl;
    cout<<"b = "<<b<<endl;
    cout<<"c = "<<c<<endl;
}
  • semi-default parameter

void Func(int a, int b = 10, int c = 20)
{
    cout<<"a = "<<a<<endl;
    cout<<"b = "<<b<<endl;
    cout<<"c = "<<c<<endl;
}

Notice:

If a semi-default parameter appears in a function parameter, it must start from the end of the formal parameter list (the last formal parameter) and specify forward. That is, the default parameters must be given sequentially from back to front and from right to left, and cannot be given at intervals.

Default parameters cannot appear in both the definition and declaration of the function, and are generally given in the declaration, because under normal circumstances, the declaration is in the header file (fun.h), and the function definition is in the source file (fun.c) , the test.c file only includes the content of the header file when compiling. If the default value is given in the source file, and the function in the test.c file is a function without a default value, then the test.c file starts with the missing Calling the function in a value-saving way will cause an error.

function overloading

concept

C++ allows several functions with the same name to declare similar functions in the same scope. Their names are the same, but the formal parameter list (parameter number, type, type order) is different.

The number of parameters is different

int add(int a, int b)
{
	return a + b;
}

int add(int a, int b, int c)
{
	return a + b + c;
}

int main()
{
	cout << add(1, 2) << endl;
	cout << add(1, 2, 3) << endl;
	return 0;
}

Different kinds of parameters

void fun(int a, int b)
{
	cout << a + b << endl;
}

void fun(double a, double b)
{
	cout << a + b << endl;
}

int main()
{
	fun(5, 6);
	fun(2.5, 8.1);
	return 0;
}

The order of the parameter types is different

void fun(int a, char b)
{
	cout << a << b << endl;
}

void fun(char a, int b)
{
	cout << a << b << endl;
}

int main()
{
	fun(5, 'a');
	fun('a', 4);
	return 0;
}

Notice:

Whether the return value of the function is the same has nothing to do with whether it constitutes function overloading. That is to say, even if the return value is different, the name and parameter list of the function are the same, which does not constitute function overloading.

The underlying principle - name mangling

In the compilation stage of C++, the function name will be modified, and the function name and parameters will be combined through a certain method. The specific modification method is different for different compilers and different operating systems.

Taking g++ under Linux as an example, the function name of void fun (int a, double b) will become _Z3funid after compilation, and void fun (int* p, int k) will become _Z3funPii. You can see that although the function The name is the same, but it is different after compilation, so that we will not conflict when calling the function

Function overloading and default functions

1. Functions with the same name, one with no parameters and one with full default

Constitute overloading, but the code is the same when calling, the compiler will report an error: multiple instances match the parameter list, the call to the overloaded function is ambiguous

2. Functions with the same name have the same parameter list, one is no default or semi-default, and the other is full default

It does not constitute overloading, because from the underlying logic analysis, their function names are the same after being modified.

Guess you like

Origin blog.csdn.net/weixin_74269833/article/details/130235730