The principle of implementing the C ++ function overload and why extern & quot; C & quot; Declaration should be added when calling a function compiled by C language in C ++ to overload extern

Source: Internet
Author: User

The principle of implementing the C ++ function overload and why the extern "C" statement must be added when calling a function compiled by C language in C ++ to overload the extern

Compared with C, function Overloading is a major feature of C ++, I believe that when using the C language, if you want to write a function to add two integer data and a floating point data addition function, the names of these two functions are definitely not the same, this will undoubtedly increase the complexity when we use this function, but we can solve this problem well in C ++, in C ++, functions support overloading, which means that the names of the two functions can be the same. This does not cause the problem of function name redefinition, however, we also need to comply with some rules when using them. These rules will be mentioned in the following discussion, next we will analyze how functions are overloaded in C ++.

Here we use the C language and C ++ to write two functions respectively, and use the symbol table of the function to observe the form of the function name after compilation.

The following is our test code:

 1 #include<iostream> 2  3 using namespace std; 4  5  6  7 int Add(int x, int y) 8  9 {10 11 int z = 0;12 13 z = x + y;14 15 return z;16 17 }18 19 20 21 double Add(double x, double y)22 23 {24 25 double z = 0;26 27 z = x + y;28 29 return z;30 31 }32 33  34 35 int main()36 37 {38 39 cout<<Add(1,3)<<endl;40 41 cout<<Add(1.5,3.5)<<endl;42 43 return 0;44 45 }

 

In the compiling environment of VS2008:

We generate the. map file, and then we can view the function name after the function is compiled as follows:

 

 

 

It is not difficult to find some rules of function naming (of course, this rule is only one-sided for the VS2008 compiling environment ):

1. Take "?" Start and end with "@ Z"

2. The function name is followed by "?" After

3. The classes behind the functions are the type modifiers of function return values and parameters in the parameter list.

 

Next we will change this same function to the C language code.

The Code is as follows:

 

 1 //#include<iostream> 2  3 //using namespace std; 4  5 #include<stdio.h> 6  7   8  9  10 11 int Add(int x, int y)12 13 {14 15 int z = 0;16 17 z = x + y;18 19 return z;20 21 }22 23  24 25 double Add(double x, double y)26 27 {28 29 double z = 0;30 31 z = x + y;32 33 return z;34 35 }36 37  38 39 int main()40 41 {42 43 //cout<<Add(1,3)<<endl;44 45 //cout<<Add(1.5,3.5)<<endl;46 47 return 0;48 49 }

 

In this case, an error occurs during compilation:

 

 

 

Here, the function name is redefined.

So why?

In this case, comment out a function, compile it, view the. map file, and view the name after the function is renamed.

 

 

 

 

Here we can find that the name renamed after the function is compiled is simply added with an underscore (_) before the function name, which makes it difficult to analyze, the function rename mechanism for C and C ++ compilation is completely different.

1. in C language, only "_" (underline) is added before the function name)

2. C ++ has its own naming and modification rules. It will modify the corresponding types based on the types of variables in the function parameter list.

Although C ++ supports function overloading, pay attention to the following points during use:

1. Function overloading only occurs in the same scope. For example, if the function names in two projects are the same, they are not function overloading.

2. The function name is the same, the function parameter list is different, and the return value can be the same but different. Why can the function return values be the same?

This is because the name modification of the function is not the same in different compiling environments. The following is the form of renaming the function after compilation in Linux:

 

 

It is not difficult to find out the rules for renaming functions in Linux:

1. Start with "_ Z" z, followed by the number of words in the function name

2. The function name is followed by the Type modifier of the parameter in the function parameter list. I is int type, and d is double type.

I believe that you can have a clear understanding of why function overloading can be implemented in C ++, then it is not difficult to answer why the extern "C" declaration should be added before calling the function compiled by C in C ++.

This is because we are currently in the c ++ environment. If we do not specify the function to be called as a function compiled in c language, the current compilation in the C ++ file will report an error saying that the function is an external symbol that cannot be parsed, during compilation and running, our current program will find the corresponding function name from the symbol table, but the function names in the C ++ and C symbol tables generated after compilation are different, this function is an external symbol that cannot be parsed, but when you use extern "C" to indicate that the function is compiled in C language, then, when the current code is compiled and run, the corresponding function name will be searched from the symbol table file compiled in C language, so that the compilation and running fee of the entire program will be no problem.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.