The principle of C + + function overloading implementation and the need to add an extern "C" declaration when calling C-compiled functions in C + +

Source: Internet
Author: User

C + + in relation to the C language support function overloading is its great feature, I believe that in the use of C language when you want to write a function to implement two integral data to add the functions of adding a floating point data, then the names of the two functions must not be the same, This undoubtedly adds complexity when we use this function, but in C + + we can solve this problem very well, because in C + + The function is overloaded, that is, the function name of the two functions can be the same, so that there is no problem of redefinition of the function name. But we have to abide by some rules when we use them, which we will mention in the next discussion, and let's analyze how functions are overloaded in C + +.

Here we use C and C + + to write two functions, through the function of the symbol table to observe the function name after compiling what is the form of

Here's our test code:

1#include <iostream>2 3 using namespacestd;4 5 6 7 intADD (intXinty)8 9 {Ten  One intz =0; A  -z = x +y; -  the returnZ; -  - } -  +  -  + DoubleADD (DoubleXDoubley) A  at { -  - Doublez =0; -  -z = x +y; -  in returnZ; -  to } +  -   the  * intMain () $ Panax Notoginseng { -  theCout<<add (1,3) <<Endl; +  ACout<<add (1.5,3.5) <<Endl; the  + return 0; -  $}

In the VS2008 compilation Environment :

We generate the. map file, and then we can view the function name as shown in the compiled function:

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

1. with "? "Start and End with" @z "

2. The name of the function is immediately "? "After

3. After function Mingde, the type modifier of the function return value type modifier, parameter list, respectively

Now let's change the 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  Ten  One intADD (intXinty) A  - { -  the intz =0; -  -z = x +y; -  + returnZ; -  + } A  at   -  - DoubleADD (DoubleXDoubley) -  - { -  in Doublez =0; -  toz = x +y; +  - returnZ; the  * } $ Panax Notoginseng   -  the intMain () +  A { the  + //Cout<<add (1,3) <<endl; -  $ //Cout<<add (1.5,3.5) <<endl; $  - return 0; -  the}

When we compile, we get an error:

This tells us that the function name is redefined

So what is this for?

Then we comment out a function and then compile and look at the. map file to see the name after the function is renamed

Here we can find that the name of the function is renamed after compiling only the function name is preceded by a "_" (underscore), so we are not difficult to analyze, C and C + + when compiling the function of the renaming mechanism is completely different

The 1.C language simply adds "_" (underscore) in front of the function's name

2.c++ has its own naming modifier, which is modified according to the type of the variable in the argument list of the function.

Although C + + supports the overloading of functions, we also pay attention to the following points when we use them:

1. The overloads of the function are only present in the same scope, for example, if the function names in the two projects are the same, but they are not overloaded with functions

2. The function name is the same, the parameter list of the function is different, the return value can be different, why the function return value can be different?

This is because the modifier of the function name is not the same in different compilation environments, the following is the form of renaming a function after the Linux environment:

It is easy to see some rules for renaming a function in a Linux environment:

1. Start with "_z" Z, followed by numbers that are the number of words in the function name

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

Through the above explained that you can be in C + + Why can implement function overloading has a clear understanding, then we will not be difficult to answer why in C + + calls by C compiled function should precede the extern "C" declaration.

This is because we are currently in the C + + language environment, this time if we do not specify that the function to be called is compiled in C language function, then the current compilation in C + + file will be an error that the function is an unresolved external symbol, Because our current program will find the corresponding function name from the symbol table when compiling and running, but the name of the function in the symbol table generated by C + + and C compile is different, then this function is an unresolved external symbol, but when you use extern "C" to indicate that the function is compiled in C language function, Then the current code in the compile run time will be compiled from the C language of the symbol table file to find the corresponding function name, so that the entire program compiled running fee is no problem.

The principle of C + + function overloading implementation and the need to add an extern "C" declaration when calling C-compiled functions in C + +

Related Article

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.