C++ review - name smashing

(1) Function overloading

(1) The prototype of the function

Function return value function name formal parameter list (parameter type and number formal parameter name can be omitted), and function body is not required.

(2) Function overloading conditions

  • under the same scope;
  • same function name;
  • The number of parameters of the function is different, the type of parameters is different, or the order is different;

Note: The return value of a function cannot be used as a condition for function overloading;

(2) Why can't C function overloading, but C++ can?

  • The C language distinguishes functions by the function name (the C compiler will name the function as _function name at the compilation stage), and if the function with the same name is used, redefinition will occur.
  • C++ distinguishes functions by the prototype of the function (the C++ compiler uses the name smashing technique)

(1) Compile with vs2019 C compiler

#include <iostream>
using namespace std;

//使用C编译器
//编译成_fun
extern "C" int Max(int a, int b)
{
	return a > b ? a : b;
}

int main()
{
	Max(10, 20);
	return 0;
}

(2) Compile with vs2019 C++ compiler

(3) C++ name crushing technology

(1) The return type cannot be used as the basis for function overloading,
 

If the return type, function name, and parameter types are all the same, but the number of parameters is different, it may not be possible to overload. If there is no default value or a default value, it can be overloaded. If there is a default value, it cannot be determined.
 Therefore, it is impossible to determine which function is called when calling, so pay attention to the ambiguity problem

Guess you like

Origin blog.csdn.net/weixin_48560325/article/details/126944350