Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to define the use of C++ function overloading

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of how to define the use of C++ function overload, the content is detailed and easy to understand, the operation is simple and fast, and it has a certain reference value. I believe that after reading this C++ function overload how to define the use of the article will have a harvest, let's take a look.

I. function overload Analysis (part I) 1.1 definition of overload

Definition: the same identifier has different meanings in different contexts

1.2 definition of function overloading

Define different functions with the same function name

The meaning of the function is different when the function name is matched with different parameters.

As follows:

Take a look at a piece of code and feel it:

# include # include int func (int x) {return x;} int func (int a, int b) {return a + b;} int func (const char* s) {return strlen (s);} int main (int argc, char* argv []) {printf ("% d\ n", func (3)); printf ("% d\ n", func (4,5)); printf ("% d\ n", func ("D.T.Software")); return 0;

The following is the output:

1.3 the conditions to be met for function overloading

Function overloading satisfies at least one of the following conditions:

The number of parameters is different

Parameter types are different

The order of parameters is different

The following figure shows that the order of the parameters is different:

Let's look at an example program in which the default parameter of a function encounters a function overload:

# include int func (int a, int b, int c = 0) {return a * b * c;} int func (int a, int b) {return a + b;} int main (int argc, char * argv []) {int c = func (1,2); return 0;}

The following is the output:

Compilation error because of ambiguity. If it is true to call the first function, the value of c has been determined because it conforms to the default parameter rules of the function; it also makes sense to call the second function, so the compilation will not pass.

1.4 guidelines for compiler calls to overloaded functions

Use all functions with the same name as candidates

Try to find a feasible candidate function

Exact matching of actual parameters

The arguments can be matched by default parameters.

Match arguments by default type conversion

Match failed

Finally, the candidate function found is not unique, then ambiguity occurs and the compilation fails.

Unable to match all candidates, function undefined, compilation failed.

1.5 precautions for function overloading

Overloaded functions are essentially different functions that are independent of each other.

Overloaded functions have different function types

The return value of a function cannot be used as a basis for function overloading.

Function overloading is determined by function name and parameter list!

What is the nature of function overloading? Following in-depth analysis of a piece of code, the compilation environment is VS2012.

# include "stdafx.h" # include int add (int a, int b) / / int (int, int) {return a + b;} int add (int a, int b, int c) / / int (int, int, int) {return a + b + c;} int main () {printf ("% p\ n", (int (*) (int, int) add); printf ("% p\ n", (int (*) (int, int, int) add)) Return 0;}

From the knowledge of C language, we can know that the function name is the entry address of the function, so the output is as follows:

As you can see, the entry addresses of the two add () functions are different, so the two add are two different functions.

What does the compiler think of these two add () functions? Let's make an in-depth analysis. First take a look at the intermediate results produced by the compiler, in the Test-> Debug-> Test.obj file.

Then use the command-line tool that comes with VS2012 to see what's in Test.obj.

The above illustration shows the location of the VS2012 command line

Enter dumpbin as follows:

All you need here is a relational SYMBOLS (symbol table), which is a table generated by the compiler according to the source code during compilation, which contains the function name variables of the program, and so on.

Enter the following command, where / symbols is followed by the location of Test.obj.

Find the place below and you can see that when the compiler compiles (int _ _ cdecl add (int,int)), the identifier is? add@@YAHHH@Z The compiler compiles (int _ cdecl add (int,int,int)) with an identifier of? add@@YAHHHH@Z, which means that the compiler has treated the two functions separately, even though they have the same name, so it is easy to understand that the entry addresses of the two add () functions are different.

1.6 Summary

Function overloading is a concept introduced in C++.

Function overloading is used to simulate lexical collocation in natural language

Function overloading makes C++ have richer semantic expression ability.

The essence of function overloading is different functions that are independent of each other.

In C++, the function call is determined by function name and function parameters.

II. Analysis of function overload (part two) 2.1 function overload meets function pointer

When assigning an overloaded function name to a function pointer

Select candidates consistent with the list of function pointer parameters according to the overloading rules

Strictly match the function type of the candidate and the function type of the function pointer

Let's look at a piece of code:

# include # include int func (int x) {return x;} int func (int a, int b) {return a + b;} int func (const char* s) {return strlen (s);} typedef int (* PFUNC) (int a); int main (int argc, char* argv []) {int c = 0; PFUNC p = func; c = p (1); printf ("c =% d\ n", c); return 0;}

The following is the output:

This is what I said earlier to select through the function type parameter list that the function pointer points to.

Matters needing attention

Function overloading must occur in the same scope

The compiler needs to use argument lists or function types for function selection.

The entry address of the overloaded function cannot be obtained directly from the function name (can be obtained through a pointer)

As follows, this code wants to get the entry address of the overloaded function through the function name:

# include int add (int a, int b) / / int (int, int) {return a + b;} int add (int a, int b, int c) / / int (int, int, int) {return a + b + c;} int main () {printf ("% p\ n", add); printf ("% p\ n", add); return 0;}

An error will be reported when compiling, and it is impossible to determine which function it is.

2.2 Mutual calls between C++ and C

In practical engineering, it is inevitable for C++ and C code to call each other.

C++ compiler is compatible with C language compilation.

C++ compiler will give priority to C++ compilation.

The extern keyword forces the C++ compiler to compile in C mode.

As follows:

Create a new 9-2 folder under the Linux environment, and first create new add.c and add.h files under the folder, as follows:

Add.c:

# include "add.h" int add (int a, int b) {return a + b;}

Add.h:

Int add (int a, int b)

Use the linux command cd to enter the 9-2 folder, and then convert add.c into an add.o file, as follows:

Then create a main.cpp file under the 9-2 folder, as follows:

Mian.cpp:

# include # include "add.h" int main () {int c = add (1d2); printf ("c =% d\ n", c); return 0;}

Compile the program, found that the program reported an error, there is no definition of the add () function, but the function has been defined, you can use the nm instruction in linux to view the information in the add.o, what is printed is the symbol table information, you can see that there is indeed add.

At this point, you need to use the extern keyword to force the C++ compiler to compile in C mode, so the main.cpp should be modified like this:

# include extern "C" {# include "add.h"} int main () {int c = add (1Magne2); printf ("c =% d\ n", c); return 0;}

In this way, the compilation will pass:

If you create a new main.c file in the 9-2 file, the code in main.c is the same as in main.cpp.

Compile, found that an error will be reported, because the extern keyword writing is C++, the C language does not support this writing. Is there a way to be compiled by both C and C++? Look at the following.

2.3 A solution that allows C code to be compiled only in the C way

_ cplusplus is a standard macro definition built into the C++ compiler

The meaning of _ cplusplus is to ensure that the C code is compiled into an object file in a uniform C way.

As follows:

So the above code can be written, main.c and main.cpp are both:

# include # ifdef _ _ cplusplusextern "C" {# endif#include "add.h" # ifdef _ cplusplus} # endifint main () {int c = add (1,2); printf ("c =% d\ n", c); return 0;}

In this way, the program can pass in both C language and C++ compilation environment, as follows:

Matters needing attention

C++ compiler cannot compile overloaded functions in C mode

The compilation method determines the target name of the compiled function name.

C++ compiles the function name and parameter list to the target name

C compilation only compiles the function name as the target name

The following is illustrated by an example:

Int add (int a, int b) / / int (int, int) {return a + b;} int add (int a, int b, int c) / / int (int, int, int) {return a + b + c;}

Compile the code into an object file, name it test.oo, and then look at the things in test.oo through the nm command in linux. You can see that there are two things T _ Z3addii and T _ Z3addiii in the test symbol table. This is the target function name after the add function is compiled, ii represents two parameters, and iii represents three parameters.

If you compile the overloaded function in C mode, the code is as follows:

Extern "C" {int add (int a, int b) / / = > add {return a + b;} int add (int a, int b, int c) / / = > add {return a + b + c;}}

The following is the compilation result. You can see the compilation error, saying that the two add () functions conflict.

This is the end of the article on "how to define the use of C++ function overloading". Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to define the use of C++ function overloading". If you want to learn more knowledge, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report