新聞中心
今天就跟大家聊聊有關(guān)c++中怎么實現(xiàn)重載函數(shù),可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
專注于為中小企業(yè)提供網(wǎng)站設(shè)計制作、做網(wǎng)站服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)江州免費做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了1000+企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
一、C++中的函數(shù)重載:
1、函數(shù)重載的概念:
用同一個函數(shù)名定義不同的函數(shù)
當(dāng)函數(shù)名和不同的參數(shù)搭配時函數(shù)的含義不同
注意:在c語言中是沒有函數(shù)重載這個概念的。
代碼示例演示:
#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()
{
return 0;
}
上面在c++編譯器里面編譯時沒有問題的,如果放在c語言編譯器里面編譯是會報錯的:
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)
所以說c語言里面不支持函數(shù)重載。
2、函數(shù)重載至少要滿足下面的一個條件:
參數(shù)個數(shù)不同
參數(shù)類型不同
參數(shù)順序不同
比如下面兩個函數(shù)可以構(gòu)造重載函數(shù)嗎?
int func (int a,const char* s)
{
return a;
}
int func(const char*s,int a)
{
return strlen(s)
}
答案肯定是可以構(gòu)造重載函數(shù)的,讀者可以自己試試(這個比較好理解)。
3、當(dāng)函數(shù)默認(rèn)參數(shù)遇上函數(shù)重載會發(fā)生什么?
例如下面的兩個函數(shù):
int func(int a, int b, int c =0)
{
return a*b*c;
}
int func(int a, int b)
{
return a+b;
}
到底會發(fā)生啥,我們還是看下面這個實驗:
#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;
}
運(yùn)行結(jié)果:
root@txp-virtual-machine:/home/txp# g++ 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)
從上面報錯的結(jié)果里面有一個單詞ambiguous(意思是夢棱兩可的),也就是說默認(rèn)參數(shù)這種使用時不允許的。
4、C++編譯器調(diào)用重載函數(shù)的準(zhǔn)則:
將所有同名函數(shù)作為候選者
嘗試尋找可行的候選函數(shù):
精確匹配實參
通過默認(rèn)參數(shù)能夠匹配實參
通過默認(rèn)類型轉(zhuǎn)換匹配實參
匹配失敗:
最終尋找到的候選函數(shù)不唯一,則出現(xiàn)二義性,編譯失敗
無法匹配所有候選者,函數(shù)未定義編譯失敗
5、函數(shù)重載的注意事項:
重載函數(shù)在本質(zhì)上是相互獨立的不同函數(shù)
重載函數(shù)的函數(shù)類型不同
函數(shù)返回值不能作為函數(shù)重載的依據(jù)
函數(shù)重載是由函數(shù)名和參數(shù)列表決定的
代碼測試:
#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;
}
運(yùn)行結(jié)果:
root@txp-virtual-machine:/home/txp# ./a.out
0x40052d
0x400541
從輸出結(jié)果我們可以看出這兩個函數(shù)的入口地址不一樣,這表明這兩個函數(shù)是不同的函數(shù)。
6、小結(jié):
函數(shù)重載是c++中引入的概念
函數(shù)重載的本質(zhì)是相互獨立的不同函數(shù)
c++中通過函數(shù)名和函數(shù)參數(shù)確定函數(shù)調(diào)用
二、重載函數(shù)進(jìn)階學(xué)習(xí)
1、重載與指針:
下面的函數(shù)指針將保存哪個函數(shù)的地址?
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 c =0;
PFUNC p = func;
c = p(2)//到底選擇哪個func函數(shù)
函數(shù)重載遇上函數(shù)指針:
將函數(shù)名賦值給函數(shù)指針時
根據(jù)重載規(guī)則跳線與函數(shù)指針參數(shù)列表一致的候選者
嚴(yán)格匹配候選者的函數(shù)類型與函數(shù)指針的函數(shù)類型
代碼試驗:
#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(2);
printf("c=%d\n",c);
return 0;
}
輸出結(jié)果:
root@txp-virtual-machine:/home/txp# ./a.out
c=2
從輸出結(jié)果來看,很明顯調(diào)用了第一個func函數(shù)。
2、注意:
函數(shù)重載必然發(fā)生在同一個作用域中
編譯器需要用參數(shù)列表或者函數(shù)類型進(jìn)行函數(shù)選擇(也就是說碰到指針,要注意函數(shù)類型了)
無法直接通過函數(shù)名得到重載函數(shù)的入口地址,這里還是通過上面的例子演示一下:
#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;
}
輸出結(jié)果:
root@txp-virtual-machine:/home/txp# g++ 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);
三、C++和C相互調(diào)用:
實際工程中C++和c代碼相互調(diào)用是不可避免的
c++編譯器能夠兼容c語言的編譯方式
c++編譯器會優(yōu)先使用c++編譯的方式
extern關(guān)鍵字能夠強(qiáng)制讓C++編譯器進(jìn)行c方式的編譯:
extern "c"
{
}
1、下面進(jìn)行一個c++中調(diào)用c函數(shù),這里我在當(dāng)前創(chuàng)建三個文件:add.c 、add.h 、main.cpp。內(nèi)容分別如下:
add.c內(nèi)容:
#include "add.h"
int add(int a, int b)
{
return a + b;
}
add.h內(nèi)容:
int add(int a, int b);
然后我用gcc編譯編譯生成add.o文件:
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
然后main.cpp里面調(diào)用add.c
#include
int main()
{
int c = add(1, 2);
printf("c = %d\n", c);
return 0;
}
輸出結(jié)果:
root@txp-virtual-machine:/home/txp/add# g++ 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
結(jié)果顯示找不到這個函數(shù),為了能夠在c++里面調(diào)用c語言里面的函數(shù),我們就要使用剛才上面講的第四點了;這里我們先用nm命令來查看一下add.o文件里面是否生成符號表(有生成):
root@txp-virtual-machine:/home/txp/add# nm add.o
0000000000000000 T add
解決方法,main.cpp改成:
#include
extern "c"
{
#include "add.h"
}
int main()
{
int c = add(1, 2);
printf("c = %d\n", c);
return 0;
}
輸出結(jié)果:
root@txp-virtual-machine:/home/txp/add# ./a.out
c = 3
2、c中如何調(diào)用c++函數(shù):
這里我把main.cpp的內(nèi)容改成:
extern "C"
{
int add(int a, int b);
}
int add(int a, int b)
{
return a+b;
}
編譯輸出:
root@txp-virtual-machine:/home/txp/add# g++ -c main.cpp -o test.o
root@txp-virtual-machine:/home/txp/add# nm -s test.o
0000000000000000 T add
add.c文件內(nèi)容改成:
#include
int main()
{
int c =0;
c = add(2,3);
printf("c=%d\n",c);
return 0;
}
輸出結(jié)果:
root@txp-virtual-machine:/home/txp/add# gcc add.c test.o
root@txp-virtual-machine:/home/txp/add# ./a.out
c=5
3、如何保證一段c代碼只會以c的方式被編譯?
解決方法如下:
__cplusplus是c++編譯器內(nèi)置的標(biāo)準(zhǔn)宏定義
__cplusplus的意義,確保c代碼以統(tǒng)一的c方式被編譯成目標(biāo)文件
#ifdef __cplusplus
extern "C"
{
#endif
#ifdef __cplusplus
}
#endif
這里把main.cpp改成:
#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;
}
輸出結(jié)果:
root@txp-virtual-machine:/home/txp/add# g++ main.cpp add.o
root@txp-virtual-machine:/home/txp/add# ./a.out
c = 3
4、注意事項
C++編譯器不能以c的方式編譯重載函數(shù)
編譯方式?jīng)Q定函數(shù)名被編譯后的目標(biāo)名
c++編譯方式將函數(shù)名和參數(shù)列表編譯成目標(biāo)名,這里舉個例子main.cpp:
int add(int a, int b)
{
return a+b;
}
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
root@txp-virtual-machine:/home/txp/add# nm test.oo
0000000000000000 T _Z3addii
0000000000000014 T _Z3addiii
說明ii表示兩個參數(shù),iii表示三個參數(shù)
c編譯方式只將函數(shù)名作為目標(biāo)名進(jìn)行編譯,這里還是以main.cpp為例:
extern "C"
{
int add(int a, int b)
{
return a+b;
}
int add(int a, int b , int c)
{
return a+b+c;
}
}
輸出結(jié)果:
root@txp-virtual-machine:/home/txp/add# g++ -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)
目標(biāo)名起沖突所以報錯。
5、小結(jié):
函數(shù)重載是c++對c的一個重要升級
函數(shù)重載通過參數(shù)列表區(qū)分不同的同名函數(shù)
extern關(guān)鍵字能夠?qū)崿F(xiàn)c和c++的相互調(diào)用
編譯方式?jīng)Q定符號表中的函數(shù)名的最終目標(biāo)名
本文參與“OSC源創(chuàng)計劃”,歡迎正在閱讀的你也加入,一起分享。
看完上述內(nèi)容,你們對c++中怎么實現(xiàn)重載函數(shù)有進(jìn)一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。
當(dāng)前題目:c++中怎么實現(xiàn)重載函數(shù)
鏈接分享:http://ef60e0e.cn/article/ppgpge.html