"C++" function overloading

  • Function overloading concept

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 parameter lists (number, type, order) of these functions with the same name must be different, and they are often used to handle the implementation. Similar functions but different data types

Just like in natural language, a word has multiple meanings, and the true meaning of the word needs to be judged according to the context. Even if the word is overloaded, the compiler in C++ will also call the function of the same name according to the situation. You can see the following Instance

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

double Add(double a,double b)
{
    
    
return a+b;
}

int main()
{
    
    
Add(1,2);
Add(1.0,2.0);//编译器根据情况自己决定调用哪个函数
}
  • Name modification
    In C/
    C++, a program needs to go through these stages to run: preprocessing, compilation, assembly
    . The name modification rules of Ca language and C++ are different. In C language, the name modification rules are particularly simple. An underscore is added in front of the function name, and C++ needs to support function overloading, namespaces, etc., which makes the C++ name modification rules more complicated. In C++, a mechanism called Name Mangling is adopted. Name Mangling is a mechanism that reorganizes the names of functions and variables during the compilation process. In simple terms, the compiler passes functions through a certain algorithm in order to distinguish between functions. , Re-decorated to a globally unique name.

  • extern "C"

The main function of extern "C" is to correctly implement C++ code calling other C language codes. After adding extern "C", the compiler will be instructed to compile this part of the code in C language instead of C++. Since C++ supports function overloading, the compiler will add the parameter types of the function to the compiled code when compiling the function, not just the function name; and the C language does not support function overloading, so compile C The function of the language code does not include the parameter type of the function, and generally includes the function name.
This function is very useful, because before the advent of C++, a lot of code was written in C language, and the very low-level library was also written in C language. In order to better support the original C code and the already written C language library, It is necessary to support C as much as possible in C++, and extern "C" is one of the strategies.

This function is mainly used in the following situations:

1. C++ code calls C language code

2. Used in C++ header files

3. In the collaborative development of multiple people, some people may be better at C language, and some people are good at C++, it will also be useful in this case

In general, the knowledge in this section is summarized into the following questions:
1. Can the following two functions form overloads?

void fun(int a=10)
{
    
    
cout<<"void fun(int)"<<endl;
}

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

No overloading, function overloading has nothing to do with the default, but with the type

2. Why can't C language support function overloading?
Because the C language's name modification rules are very simple, the function name will also cause the compiler to be unable to distinguish

3. How is the bottom layer of function overloading handled in C++?
C++ uses name mangling (tilting) technology to change the function name and distinguish functions of the same name with different parameters

4. Can a function be compiled in C style in C++?
Yes, add extern "C" in front of the function to tell the compiler to compile the function according to the rules of C language

Guess you like

Origin blog.csdn.net/NanlinW/article/details/103033661