In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Today, I will talk to you about how to achieve overloaded functions in C++, which may not be well understood by many people. in order to make you understand better, the editor has summarized the following. I hope you can get something from this article.
1. Functions in C++ are overloaded:
1. The concept 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.
Note: there is no concept of function overloading in C language.
The code example demonstrates:
# include
# include
Int func (int x)
{
Return x
}
Int func (int a, int b)
{
Return (aquib)
}
Int func (const char* s)
{
Return strlen (s)
}
Int main ()
{
Return 0
}
Above, there is no problem when compiling in the C++ compiler. If you put it in the C language compiler, it will report an error:
Root@txp-virtual-machine:/home/txp# gcc test5.c
Test5.c:8:5: error: conflicting types for 'func'
Int func (int a, int b)
^
Test5.c:3:5: note: previous definition of 'func' was here
Int func (int x)
^
Test5.c:13:5: error: conflicting types for 'func'
Int func (const char* s)
^
Test5.c:3:5: note: previous definition of 'func' was here
Int func (int x)
So function overloading is not supported in the c language.
2. Function overloading must meet at least one of the following conditions:
The number of parameters is different
Parameter types are different
The order of parameters is different
For example, can the following two functions construct overloaded functions?
Int func (int a minute Const char* s)
{
Return a
}
Int func (const char*s,int a)
{
Return strlen (s)
}
The answer must be to construct overloaded functions, which readers can try for themselves (this is easier to understand).
3. What happens when the default parameter of a function is overloaded?
For example, the following two functions:
Int func (int a, int b, int c = 0)
{
Return a*b*c
}
Int func (int a, int b)
{
Return aquib
}
What will happen? let's take a look at the following experiment:
# 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
}
Running result:
Root@txp-virtual-machine:/home/txp# gathers + test5.cpp
Test5.cpp: In function 'int main (int, char**)':
Test5.cpp:16:22: error: call of overloaded 'func (int, int)' is ambiguous
Int c = func (1,2)
^
Test5.cpp:16:22: note: candidates are:
Test5.cpp:3:5: note: int func (int, int, int)
Int func (int a, int b, int c = 0)
^
Test5.cpp:8:5: note: int func (int, int)
Int func (int a, int b)
The result of the error reported above includes the word ambiguous (meaning ambiguous), which means that the default parameter is not allowed to be used.
4. The criteria for C++ compiler to call overloaded functions:
Use all functions with the same name as candidates
Try to find a viable candidate function:
Exact matching of actual parameters
The arguments can be matched by default parameters.
Match arguments by default type conversion
Match failed:
If the candidate function is not unique, it will be ambiguous and the compilation will fail.
Unable to match all candidates, function undefined compilation failed
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.
Code testing:
# 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
}
Running result:
Root@txp-virtual-machine:/home/txp#. / a.out
0x40052d
0x400541
From the output, we can see that the entry addresses of the two functions are different, which indicates that the two functions are different functions.
6. Summary:
Function overloading is a concept introduced in C++.
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.
Second, advanced learning of heavy-loaded functions
1. Overload and pointer:
Which function's address will be saved by the following function pointer?
Int func (int x)
{
Return x
}
Int func (int a, int b)
{
Return aquib
}
Int func (const char* s)
{
Return strlen (s)
}
Typedef int (* PFUNC) (int a)
Int c = 0
PFUNC p = func
C = p (2) / / which func function is chosen?
Function overload encounters function pointer:
When assigning a function name to a function pointer
Candidates whose jumpers are consistent with the list of function pointer arguments according to the overload rule
Strictly match the function type of the candidate and the function type of the function pointer
Code test:
# include
# include
Int func (int x)
{
Return x
}
Int func (int a, int b)
{
Return aquib
}
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 (2)
Printf ("cased% d\ n", c)
Return 0
}
Output result:
Root@txp-virtual-machine:/home/txp#. / a.out
Cymb2
From the output, it is clear that the first func function is called.
2. Note:
Function overloading must occur in the same scope
The compiler needs to use argument lists or function types for function selection (that is, when it comes to pointers, pay attention to function types)
The entry address of the overloaded function cannot be obtained directly from the function name. Here is a demonstration through the example above:
# 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
}
Output result:
Root@txp-virtual-machine:/home/txp# gathers + test5.cpp
Test5.cpp: In function 'int main ()':
Test5.cpp:15:23: error: overloaded function with no contextual type information
Printf ("% p\ n", add)
^
Test5.cpp:16:23: error: overloaded function with no contextual type information
Printf ("% p\ n", add)
Third, C++ and C call each other:
It is inevitable for C++ and c codes to call each other in practical projects.
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:
Extern "c"
{
}
1. The following is a C++ call to the c function. Here I create three files: add.c, add.h, and main.cpp. The contents are as follows:
Add.c content:
# include "add.h"
Int add (int a, int b)
{
Return a + b
}
Add.h content:
Int add (int a, int b)
Then I compiled and compiled the add.o file with gcc:
Root@txp-virtual-machine:/home/txp/add# vim add.c
Root@txp-virtual-machine:/home/txp/add# vim add.h
Root@txp-virtual-machine:/home/txp/add# gcc-c add.c-o add.o
Root@txp-virtual-machine:/home/txp/add# ls
Add.c add.h add.o
Then add.c is called in main.cpp.
# include
Int main ()
{
Int c = add (1,2)
Printf ("c =% d\ n", c)
Return 0
}
Output result:
Root@txp-virtual-machine:/home/txp/add# gathers + main.cpp add.o
/ tmp/ccqz3abQ.o: In function `main':
Main.cpp: (.text + 0x13): undefined reference to `add (int, int)'
Collect2: error: ld returned 1 exit status
The result shows that this function cannot be found. In order to call functions in language c in C++, we need to use the fourth point mentioned above. Here, we first use the nm command to check whether a symbol table is generated (yes) in the add.o file:
Root@txp-virtual-machine:/home/txp/add# nm add.o
0000000000000000 T add
The solution is to change main.cpp to:
# include
Extern "c"
{
# include "add.h"
}
Int main ()
{
Int c = add (1,2)
Printf ("c =% d\ n", c)
Return 0
}
Output result:
Root@txp-virtual-machine:/home/txp/add#. / a.out
C = 3
2. How to call C++ function in c:
Here I change the content of main.cpp to:
Extern "C"
{
Int add (int a, int b)
}
Int add (int a, int b)
{
Return aquib
}
Compile output:
Root@txp-virtual-machine:/home/txp/add# gathers +-c main.cpp-o test.o
Root@txp-virtual-machine:/home/txp/add# nm-s test.o
0000000000000000 T add
The content of the add.c file is changed to:
# include
Int main ()
{
Int c = 0
C = add (2pr 3)
Printf ("cased% d\ n", c)
Return 0
}
Output result:
Root@txp-virtual-machine:/home/txp/add# gcc add.c test.o
Root@txp-virtual-machine:/home/txp/add#. / a.out
Cedar 5
3. How to ensure that a piece of c code will only be compiled in c way?
The solution is as follows:
_ _ cplusplus is a standard macro definition built into the C++ compiler
The meaning of _ _ cplusplus to ensure that the c code is compiled into an object file in a uniform c manner
# ifdef _ _ cplusplus
Extern "C"
{
# endif
# ifdef _ _ cplusplus
}
# endif
Here, change main.cpp to:
# include
# ifdef _ _ cplusplus
Extern "C" {
# endif
# include "add.h"
# ifdef _ _ cplusplus
}
# endif
Int main ()
{
Int c = add (1,2)
Printf ("c =% d\ n", c)
Return 0
}
Output result:
Root@txp-virtual-machine:/home/txp/add# gathers + main.cpp add.o
Root@txp-virtual-machine:/home/txp/add#. / a.out
C = 3
4. Points for attention
The C++ compiler cannot compile overloaded functions in the way c
The compilation method determines the target name of the compiled function name.
C++ compiles the function name and parameter list to the target name. Here's an example: main.cpp:
Int add (int a, int b)
{
Return aquib
}
Int add (int a, int b, int c)
{
Return a+b+c
}
Compile output:
Root@txp-virtual-machine:/home/txp/add# gathers +-c main.cpp-o test.oo
Root@txp-virtual-machine:/home/txp/add# nm test.oo
0000000000000000 T _ Z3addii
0000000000000014 T _ Z3addiii
Note that ii represents two parameters and iii represents three parameters
C compilation only compiles the function name as the target name. Here, take main.cpp as an example:
Extern "C"
{
Int add (int a, int b)
{
Return aquib
}
Int add (int a, int b, int c)
{
Return a+b+c
}
}
Output result:
Root@txp-virtual-machine:/home/txp/add# gathers +-c main.cpp-o test.oo
Main.cpp: In function 'int add (int, int, int)':
Main.cpp:8:29: error: declaration of C function 'int add (int, int, int)' conflicts with
Int add (int a, int b, int c)
^
Main.cpp:3:5: error: previous declaration 'int add (int, int)' here
Int add (int a, int b)
The target name conflicts, so an error is reported.
5. Summary:
Function overloading is an important upgrade of C++ to c
Function overloading distinguishes different functions of the same name by parameter lists.
The extern keyword can realize the mutual call between c and C++.
The compilation method determines the final target name of the function name in the symbol table
This article participates in the "OSC Source Program". You are welcome to join us and share it.
After reading the above, do you have any further understanding of how to implement overloaded functions in C++? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.