“This is the 8th day of my participation in the First Challenge 2022. For details: First Challenge 2022”

C++ C tools began to fall out of hand

The default parameters

Default Parameter Concept

A default parameter specifies a default value for a function parameter when declaring or defining a function. This is the default value. When the function is called, the default value is used if no argument is specified; otherwise, the specified argument is used

Default parameter classification

All default parameters All parameters are given default values

image-20211205135858443



Semi-default parameters Semi-default parameters must be given from right to left, not at intervals

image-20211205142328841


  1. Semi-default parameters are requiredFrom right to leftSo, in turn,You can’t give them at intervals
  2. Default parameters cannot be declared and defined in functionsSimultaneous occurrence ofEither in the declaration, or in the definition, it is recommended to write in the declaration
  3. The default value must be a constant or global variable
  4. C language not supported (compiler not supported)
image-20211205155024828



Function overloading

Function overload concept

Function overloading is a special case of a function. C++ allows several functions of the same name to be declared in the same scope. These functions must have different parameter lists (parameter number or type or order)

image-20211205164343460


#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<assert.h>
using namespace std;

void fun(int a, char b)
{
	cout << a << endl;
	cout << b << endl << endl;
}
// Different parameter types
void fun(double a, double b)
{
	cout << a << endl;
	cout << b << endl << endl;
}
// Different number of parameters
void fun(int a)
{
	cout << a << endl << endl;
}
// Different order of arguments
void fun(char b, int a)
{
	cout << a << endl;
	cout << b << endl << endl;
}
int main(a)
{
	fun(10.'a');
	fun(10.1.10.1);
	fun(10);
	fun('a'.10);
	return 0;
}
Copy the code

Function overloading is all about the type, the number, the order, because those three are calling gates, just like just returning different values doesn’t count as overloading because of calling gates, so the compiler doesn’t know what to get into, so it doesn’t return.

What’s really interesting is the combination of default arguments and function overloading

image-20211205171516109



The underlying principle of function overloading is that another set of tools has been developed

Let’s see if the C language supports overloading

image-20211205210328382


We can see that the C language does not support function overloading

So let’s try masking a function

image-20211205211833171


Let’s see if c++ supports overloading

C++ compatible with C, this program can also be compiled with g++ (C++ compiler)

image-20211205212809998


Vs compiles differently from Linux

Vs calls the corresponding compiler according to the file suffix. C is to compile C. CPP is to compile C ++

Linux doesn’t use file suffixes. GCC means c g++ means c++


Why c doesn’t support function overloading, but C ++ does? So how does c++ support it?

Review how the compiler compiles links

fun.c fun.h test.c

image-20211206010512058


How does c++ support function overloading

C++ object file symbol table does not directly use function names to identify and find functions

1. The function name decorates the rule, but this decorates the rule differently from compiler to compiler

2. With the function name modifier rule, there is no ambiguity for overloaded functions in fun.o as long as the parameters are different

3. The main function of test.o calls two overloaded functions to find the address

image-20211206010722313


Linux under the assembly out of the function naming rules

_Z prefix + function name length + function name + argument type first letter

We can see that this naming convention has nothing to do with the return value

The tool for viewing Linux compilations is objdump -s

Vs under the assembly of the function naming rules

It’s hard to understand. I don’t want to read it. Let’s find some information for you

image-20211206021144139


image-20211206021417815