|
一、C++中的函数重载: 1、函数重载的概念:
代码示例演示: #include <stdio.h>2 t5 @& W! E) j#include <string.h> int func(int x) {7 b5 i, a) t/ c return x;( p/ ?' C$ @$ f h- j } 8 f* q2 }3 }' t3 g% ]' y int func(int a, int b) { return(a+b);9 H7 ?% g, `) N7 l# w. Z2 g } int func(const char* s) { return strlen(s); } : q& X6 V- F" T# [9 A int main() {5 V5 L: i5 j T6 Z , @! o% k9 l$ d1 O0 V return 0; } 6 `; H: _7 ]7 T& w! d- y 上面在c++编译器里面编译时没有问题的,如果放在c语言编译器里面编译是会报错的: root@txp-virtual-machine:/home/txp# gcc test5.ctest5.c:8:5: error: conflicting types for ‘func’ int func(int a, int b) ^ test5.c:3:5: note: previous definition of ‘func’ was here4 r$ ?7 i4 m5 S: e 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 here2 O' R- `! `( x' K6 ^ p int func(int x)! Y, A$ m- V3 W" B% x$ ~0 U& o * e R0 J% u% |+ k- f 所以说c语言里面不支持函数重载。 2、函数重载至少要满足下面的一个条件:
比如下面两个函数可以构造重载函数吗? int func (int a,const char* s){/ p B) y# P& [. G2 K return a; } 2 \2 I2 t6 c+ ~6 l int func(const char*s,int a) { return strlen(s)$ q% V' i, F+ L2 i0 x: b } 答案肯定是可以构造重载函数的,读者可以自己试试(这个比较好理解)。 3、当函数默认参数遇上函数重载会发生什么? 例如下面的两个函数: int func(int a, int b, int c =0){ return a*b*c;( I9 [# G; T6 S9 c) B }. ^! a+ X, L9 i6 |0 E4 o1 m0 w0 h int func(int a, int b) `2 p }- X! o8 c& j { return a+b;- U0 b0 \# K& q8 n. d } 到底会发生啥,我们还是看下面这个实验: #include <stdio.h>1 W Q& [. _! `4 z' V, s5 aint func(int a, int b, int c = 0) { return a * b * c;% `$ d+ \( s- I } int func(int a, int b): f# U8 s% L4 T3 t, R7 @ { return a + b;- C" N+ Q i9 V. _. d( B }( a! K( |: c2 p, f: ]6 e int main(int argc, char *argv[])6 @ |; X/ u: M' X0 y, L& L9 Y {: T, L! J% Z; M3 a) { int c = func(1, 2);- h) Z, f, n, B1 j# ~ $ H, y8 T9 P. z( ?4 D- m+ u ~ return 0; }1 K3 a, [4 x6 L" \8 p4 U " A7 ~# H0 z4 ^$ i4 ] 运行结果: root@txp-virtual-machine:/home/txp# g++ test5.cpptest5.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)% ~5 h+ k4 a3 o) _3 {" H. j1 s ^ test5.cpp:8:5: note: int func(int, int) int func(int a, int b) @' x! F7 U6 b8 d+ x5 P 从上面报错的结果里面有一个单词ambiguous(意思是梦棱两可的),也就是说默认参数这种使用时不允许的。 4、C++编译器调用重载函数的准则:
通过默认参数能够匹配实参 通过默认类型转换匹配实参
最终寻找到的候选函数不唯一,则出现二义性,编译失败 无法匹配所有候选者,函数未定义编译失败 5、函数重载的注意事项:
代码测试: #include <stdio.h>% X/ J0 |6 \7 d0 v$ ?. e~5 y+ Q3 H0 \( _ K. t/ d int add(int a, int b) // int(int, int)- K$ @* d7 }2 f3 \" N( M { return a + b; } 6 l6 q$ P% j. J int add(int a, int b, int c) // int(int, int, int)0 E% G8 W' K5 o5 P* U { return a + b + c;* j; ?! K+ p" M6 C K4 S } int main() {/ W4 q+ e( w6 \ printf("%p\n", (int(*)(int, int))add);$ h# E5 ]6 N+ ^7 M2 X. c2 |/ a7 i printf("%p\n", (int(*)(int, int, int))add); 9 {1 b$ ?% W6 Z3 J" T& X/ M return 0;1 o# `# m& p" e } 运行结果: root@txp-virtual-machine:/home/txp# ./a.out0x40052d, J5 G' q5 s' E! S: J& w( O 0x400541& {7 J3 l* ~7 ?# c, V 从输出结果我们可以看出这两个函数的入口地址不一样,这表明这两个函数是不同的函数。 6、小结:
二、重载函数进阶学习 1、重载与指针: 下面的函数指针将保存哪个函数的地址? int func(int x){, a# C% q" G- u% q5 l return x;$ w7 c1 ]5 t' ~8 Z6 M( T6 K' c" b1 L3 ? }! _; {; r: m: I8 j$ X/ M0 | int func(int a, int b)9 f% _' Q; b( E& N( h: i { return a+b; } int func(const char* s) { return strlen(s);. g | V, O) }- u8 A. d } - |+ J- b3 m) f) F: g: B ]9 v typedef int (*PFUNC) (int a); int c =0;% }8 d9 k8 s3 l PFUNC p = func;1 z( X2 S7 l, B$ [- K w, s/ a+ ]. c5 d , H8 x- k% \. b6 A8 c. g c = p(2)//到底选择哪个func函数 % q4 Z' z7 z. t3 f) X& k$ E$ k 函数重载遇上函数指针:
代码试验: #include <stdio.h>#include <string.h> 2 O7 i7 y2 Z8 F3 f* q- R& @ int func(int x) {5 X8 P: p, Z, D& D return x; }/ V( C) a# a7 W1 Q 7 w+ {* |% E c$ u int func(int a, int b) { return a+b;5 m+ R3 g( u) I" a% D } int func(const char* s)8 j/ W& h4 l0 d* N8 Q0 V { return strlen(s);/ X8 H0 _+ d% Y1 J$ v' {3 W% w }7 R4 A. K5 D' ~ P5 j typedef int(*PFUNC)(int a);( x" b9 p1 L& b0 M. b' e + r) z4 F# E: V int main(int argc,char *argv[])) \$ j) U+ Z8 g; Q8 j {+ n3 e0 _3 i+ b/ k int c =0; B& P' D1 h" V PFUNC p =func;4 r' @) P q' t# E4 U5 y * h+ a1 m$ b% o- D0 R2 j Z3 Y c = p(2);+ R, J- X& R) v j, L* } printf("c=%d\n",c);5 ~( { k3 L* }; @" n return 0;# O4 t9 F0 M o } . a! e9 J; p$ g/ k* l( ]3 y0 Z6 Z 输出结果: root@txp-virtual-machine:/home/txp# ./a.out' x1 K7 `; k" i3 ^2 Z, V0 [c=2 % K) s% C; E. G8 f+ | 从输出结果来看,很明显调用了第一个func函数。 2、注意:
无法直接通过函数名得到重载函数的入口地址,这里还是通过上面的例子演示一下: #include <stdio.h>. H3 N. {& Z- j5 \" o2 K+ P int add(int a, int b) // int(int, int)% R" N+ m8 z) ~ {; |( H+ ^4 g5 Y+ w9 W$ N return a + b; } int add(int a, int b, int c) // int(int, int, int)1 H- n4 O' I# J: J8 `$ [ y* A { return a + b + c; }1 @2 V4 U! ~2 t int main() {# h" h2 {1 y9 M" b4 D% |6 v printf("%p\n", add); printf("%p\n", add);4 {0 n; f2 l+ X$ w5 U S0 s5 { * t( o4 D6 o: E: e2 [ return 0; }6 W; I5 }: x: o) x5 M 输出结果: root@txp-virtual-machine:/home/txp# g++ test5.cpp; ~6 W) _ T/ M, m9 Ftest5.cpp: In function ‘int main()’: test5.cpp:15:23: error: overloaded function with no contextual type information/ V/ _$ y; E7 a6 W) u printf("%p\n", add); ^ test5.cpp:16:23: error: overloaded function with no contextual type information: O. B2 p$ B0 U3 |& y printf("%p\n", add);' r# A |+ c- T+ V 6 w6 J: M& G! R3 M 三、C++和C相互调用:
extern关键字能够强制让C++编译器进行c方式的编译: extern "c"+ x) P* d3 K7 Q6 r2 Z{- c! X9 H$ E3 B& p. V }# m9 t" r" U3 { 1、下面进行一个c++中调用c函数,这里我在当前创建三个文件:add.c 、add.h 、main.cpp。内容分别如下: add.c内容: 8 ^7 G6 ]+ h; h% Q#include "add.h" int add(int a, int b) 6 p' o5 R; F7 Q0 h& I* w, I+ y+ V# y {# n1 J1 n/ i. S+ ^! _1 z! x return a + b; 1 ?$ }5 W( N9 \0 S } 1 Y; [3 {* y7 d' P ]+ b$ X t add.h内容: int add(int a, int b);然后我用gcc编译编译生成add.o文件: root@txp-virtual-machine:/home/txp/add# vim add.croot@txp-virtual-machine:/home/txp/add# vim add.h root@txp-virtual-machine:/home/txp/add# gcc -c add.c -o add.o$ u7 `; ]! Q/ h$ p. u0 a4 t root@txp-virtual-machine:/home/txp/add# ls add.c add.h add.o& p S- D( c0 E* _5 c0 ^ 然后main.cpp里面调用add.c #include <stdio.h>int main()% G7 X% L v$ t$ O: k7 s; L { int c = add(1, 2); printf("c = %d\n", c); return 0;- P( }7 B ^- l3 F Y }- E8 U1 ? r' D1 B2 A 输出结果: root@txp-virtual-machine:/home/txp/add# g++ main.cpp add.o/tmp/ccqz3abQ.o: In function `main':4 I- V# _/ J, a: _( H) N4 d main.cpp .text+0x13): undefined reference to `add(int, int)'collect2: error: ld returned 1 exit status. Z3 w) M' y6 m7 o( e 4 n; s$ \3 b: F( R: q 6 l6 |) p- F5 w v5 d4 A/ B5 I! l 结果显示找不到这个函数,为了能够在c++里面调用c语言里面的函数,我们就要使用刚才上面讲的第四点了;这里我们先用nm命令来查看一下add.o文件里面是否生成符号表(有生成): root@txp-virtual-machine:/home/txp/add# nm add.o3 o, I1 l& f1 V0000000000000000 T add) J1 l1 k4 l+ `3 G 解决方法,main.cpp改成: #include <stdio.h>extern "c" {# J3 b0 d+ G# Z% c( }* v" ` #include "add.h" } int main() { int c = add(1, 2);5 ]5 M O( C' g, c; j & n1 z' Y0 M9 s ^ P printf("c = %d\n", c);2 H7 u8 A1 U* X return 0; }& F, y' o- L4 e1 v 输出结果: root@txp-virtual-machine:/home/txp/add# ./a.outc = 3 6 x4 K, E5 l8 R! \( x& n/ v 2、c中如何调用c++函数: 这里我把main.cpp的内容改成: extern "C"{, v9 v6 A% F, ?! r int add(int a, int b);: z9 m' f) a) d: [. @8 E } " d% }$ |& [/ L+ L' e; F, S int add(int a, int b) V- T( r3 B* A {. l" `8 R3 b! d# M return a+b; : M8 o, k( j+ w } " X" o$ V3 ^. o " B! v! n# p% o* T+ w4 r' B# V+ f. x6 V 编译输出: root@txp-virtual-machine:/home/txp/add# g++ -c main.cpp -o test.o! S ?5 n( U" L3 ]root@txp-virtual-machine:/home/txp/add# nm -s test.o' k3 m( {* w5 l9 L+ n+ ], U 0000000000000000 T add ; Z Q$ `" a9 o2 j1 g 4 j) G! X8 U- k% M, B; I add.c文件内容改成: #include <stdio.h>int main() { int c =0;7 k' C7 r/ o# _( p c = add(2,3); printf("c=%d\n",c);8 M0 X4 L. `5 X! Z' h( [ return 0; }) @! W' J s4 J/ C" t 输出结果: root@txp-virtual-machine:/home/txp/add# gcc add.c test.oroot@txp-virtual-machine:/home/txp/add# ./a.out. ]3 D2 I0 S$ Y/ J0 ?9 [7 G# r c=5* M! j# Z1 x$ l' ?$ x; |' m 3、如何保证一段c代码只会以c的方式被编译? 解决方法如下:
extern "C"; Y# ?$ @9 v* q$ Z3 d {5 p) Q& X) }- D2 G #endif) s) _3 O3 u0 L7 E" C" @ #ifdef __cplusplus7 ^9 |- o" x6 }# H: T! W }& g H( R! T" h7 I4 e2 v, G1 y7 f, I+ { #endif4 ?. Y: h" s" y4 T5 o$ W% h& ^ 这里把main.cpp改成: #include <stdio.h>#ifdef __cplusplus extern "C" { #endif #include "add.h" #ifdef __cplusplus# ~ B2 v( F$ x9 h } #endif" H0 g/ M6 K1 ^: o/ ]2 \! v int main() { int c = add(1, 2); printf("c = %d\n", c); 0 _- I8 K3 w# A2 i return 0; } 输出结果: root@txp-virtual-machine:/home/txp/add# g++ main.cpp add.oroot@txp-virtual-machine:/home/txp/add# ./a.out c = 37 \5 r1 c3 H# V. Z- Z, a# m 4、注意事项
{ return a+b; } & }- r( f1 r/ g int add(int a, int b , int c)/ C9 Q. a# c9 X$ K7 K {6 ^1 x% S0 A4 _3 G2 ?3 M return a+b+c;* H8 X/ X* \, a } 编译输出: root@txp-virtual-machine:/home/txp/add# g++ -c main.cpp -o test.ooroot@txp-virtual-machine:/home/txp/add# nm test.oo" {$ M+ L" U7 u: O' v1 k; ] 0000000000000000 T _Z3addii0 w3 M) {+ q, R5 Z 0000000000000014 T _Z3addiii 0 q8 x& F4 V) Q* _; H+ p9 l 说明ii表示两个参数,iii表示三个参数
{ int add(int a, int b) { return a+b;" W, ]0 N' B: W0 W2 {" @& ~ }& o7 o; v+ l) R% H * j. p9 S" Z& G4 {( X int add(int a, int b , int c) { return a+b+c; } } 输出结果: root@txp-virtual-machine:/home/txp/add# g++ -c main.cpp -o test.oo: M" B& y8 T: l% X% K' tmain.cpp: In function ‘int add(int, int, int)’: `$ [' N: t8 E9 p main.cpp:8:29: error: declaration of C function ‘int add(int, int, int)’ conflicts with int add(int a, int b , int c)+ j# K$ ^4 k. h3 q ^ main.cpp:3:5: error: previous declaration ‘int add(int, int)’ here/ s/ T5 Y+ w1 b! A int add(int a, int b) 目标名起冲突所以报错。 5、小结:
四、总结:. d! R3 q, j# D' Z6 Z; T 好了,今天的分享就到这里,如果文章中有错误或者不理解的地方,可以交流互动,一起进步。 & @! w& O7 F: H0 e |
微信公众号
手机版