#include <iostream>
using namespace std;
void swap(int &v1,int &v2) {
int temp = v1;
v1 = v2;
v2 = temp;
}
void swap(char &v1, char &v2) {
char temp = v1;
v1 = v2;
v2 = temp;
}
int main() {
int a = 1, b = 2;
swap(a, b);
cout << "a=" << a << ",b=" << b << endl;
char c = 'q', d = 'w';
swap(c, d);
cout << "c=" << c << ",d=" << d << endl;
return 0;
}
函數(shù)的返回值類型與函數(shù)重載無(wú)關(guān)。
創(chuàng)新互聯(lián)建站專注于南溪企業(yè)網(wǎng)站建設(shè),自適應(yīng)網(wǎng)站建設(shè),購(gòu)物商城網(wǎng)站建設(shè)。南溪網(wǎng)站建設(shè)公司,為南溪等地區(qū)提供建站服務(wù)。全流程按需網(wǎng)站開(kāi)發(fā),專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,創(chuàng)新互聯(lián)建站專業(yè)和態(tài)度為您提供的服務(wù)
調(diào)用函數(shù)時(shí),實(shí)參的隱式類型轉(zhuǎn)換可能會(huì)產(chǎn)生二義性。
#include <iostream>
using namespace std;
void display(int v1) {
cout << "display(int)" << v1 << endl;
}
void display(char v1, int v2) {
cout << "display(char)" << v1 << "," << v2 << endl;
}
int main() {
display(1);
display('a',2);
return 0;
}
然后進(jìn)行編譯生成(注意:在編譯生成的時(shí)候要把debug模式改為release模式,并且要禁止release模式的優(yōu)化),如下圖:
再將生成好的release版exe文件使用IDA打開(kāi),由下圖我們可以看到,兩函數(shù)名是不同的,這也就印證了我們以上的說(shuō)法。
當(dāng)前題目:C++函數(shù)重載的原理
網(wǎng)頁(yè)網(wǎng)址:http://chinadenli.net/article24/dsoijje.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供云服務(wù)器、網(wǎng)站導(dǎo)航、企業(yè)網(wǎng)站制作、網(wǎng)站設(shè)計(jì)公司、標(biāo)簽優(yōu)化、微信公眾號(hào)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容
移動(dòng)網(wǎng)站建設(shè)知識(shí)