欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

c++中怎么實(shí)現(xiàn)重載函數(shù)

今天就跟大家聊聊有關(guān)c++中怎么實(shí)現(xiàn)重載函數(shù),可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

專注于為中小企業(yè)提供網(wǎng)站設(shè)計(jì)制作、做網(wǎng)站服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)江州免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了1000+企業(yè)的穩(wěn)健成長(zhǎng),幫助中小企業(yè)通過(guò)網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。

一、C++中的函數(shù)重載:

1、函數(shù)重載的概念:

  • 用同一個(gè)函數(shù)名定義不同的函數(shù)

  • 當(dāng)函數(shù)名和不同的參數(shù)搭配時(shí)函數(shù)的含義不同

  • 注意:在c語(yǔ)言中是沒(méi)有函數(shù)重載這個(gè)概念的。

代碼示例演示:

#include <stdio.h>
#include <string.h>
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++編譯器里面編譯時(shí)沒(méi)有問(wèn)題的,如果放在c語(yǔ)言編譯器里面編譯是會(huì)報(bào)錯(cuò)的:

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)

 

所以說(shuō)c語(yǔ)言里面不支持函數(shù)重載。

2、函數(shù)重載至少要滿足下面的一個(gè)條件:

  • 參數(shù)個(gè)數(shù)不同

  • 參數(shù)類型不同

  • 參數(shù)順序不同

比如下面兩個(gè)函數(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ù)的,讀者可以自己試試(這個(gè)比較好理解)。

3、當(dāng)函數(shù)默認(rèn)參數(shù)遇上函數(shù)重載會(huì)發(fā)生什么?

例如下面的兩個(gè)函數(shù):

int func(int a, int b, int c =0)
{
   return a*b*c;
}
int func(int a, int b)
{
  return a+b;
}

 

到底會(huì)發(fā)生啥,我們還是看下面這個(gè)實(shí)驗(yàn):

#include <stdio.h>

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)

 

從上面報(bào)錯(cuò)的結(jié)果里面有一個(gè)單詞ambiguous(意思是夢(mèng)棱兩可的),也就是說(shuō)默認(rèn)參數(shù)這種使用時(shí)不允許的。

4、C++編譯器調(diào)用重載函數(shù)的準(zhǔn)則:

  • 將所有同名函數(shù)作為候選者

  • 嘗試尋找可行的候選函數(shù):

  • 精確匹配實(shí)參

       通過(guò)默認(rèn)參數(shù)能夠匹配實(shí)參

      通過(guò)默認(rèn)類型轉(zhuǎn)換匹配實(shí)參

  • 匹配失?。?/p>

      最終尋找到的候選函數(shù)不唯一,則出現(xiàn)二義性,編譯失敗

      無(wú)法匹配所有候選者,函數(shù)未定義編譯失敗

5、函數(shù)重載的注意事項(xiàng):

  • 重載函數(shù)在本質(zhì)上是相互獨(dú)立的不同函數(shù)

  • 重載函數(shù)的函數(shù)類型不同

  • 函數(shù)返回值不能作為函數(shù)重載的依據(jù)

  • 函數(shù)重載是由函數(shù)名和參數(shù)列表決定的

代碼測(cè)試:

#include <stdio.h>

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é)果我們可以看出這兩個(gè)函數(shù)的入口地址不一樣,這表明這兩個(gè)函數(shù)是不同的函數(shù)。

6、小結(jié):

  • 函數(shù)重載是c++中引入的概念

  • 函數(shù)重載的本質(zhì)是相互獨(dú)立的不同函數(shù)

  • c++中通過(guò)函數(shù)名和函數(shù)參數(shù)確定函數(shù)調(diào)用

二、重載函數(shù)進(jìn)階學(xué)習(xí)

1、重載與指針:

下面的函數(shù)指針將保存哪個(gè)函數(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)//到底選擇哪個(gè)func函數(shù)

 

函數(shù)重載遇上函數(shù)指針:

  • 將函數(shù)名賦值給函數(shù)指針時(shí)

  • 根據(jù)重載規(guī)則跳線與函數(shù)指針參數(shù)列表一致的候選者

  • 嚴(yán)格匹配候選者的函數(shù)類型與函數(shù)指針的函數(shù)類型

代碼試驗(yàn):

#include <stdio.h>
#include <string.h>

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é)果來(lái)看,很明顯調(diào)用了第一個(gè)func函數(shù)。

2、注意:

  • 函數(shù)重載必然發(fā)生在同一個(gè)作用域中

  • 編譯器需要用參數(shù)列表或者函數(shù)類型進(jìn)行函數(shù)選擇(也就是說(shuō)碰到指針,要注意函數(shù)類型了)

無(wú)法直接通過(guò)函數(shù)名得到重載函數(shù)的入口地址,這里還是通過(guò)上面的例子演示一下:

#include <stdio.h>

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)用:

  • 實(shí)際工程中C++和c代碼相互調(diào)用是不可避免的

  • c++編譯器能夠兼容c語(yǔ)言的編譯方式

  • c++編譯器會(huì)優(yōu)先使用c++編譯的方式

extern關(guān)鍵字能夠強(qiáng)制讓C++編譯器進(jìn)行c方式的編譯:

extern "c"
{


}

 

1、下面進(jìn)行一個(gè)c++中調(diào)用c函數(shù),這里我在當(dāng)前創(chuàng)建三個(gè)文件: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 <stdio.h>
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é)果顯示找不到這個(gè)函數(shù),為了能夠在c++里面調(diào)用c語(yǔ)言里面的函數(shù),我們就要使用剛才上面講的第四點(diǎn)了;這里我們先用nm命令來(lái)查看一下add.o文件里面是否生成符號(hào)表(有生成):

root@txp-virtual-machine:/home/txp/add# nm add.o
0000000000000000 T add

 

解決方法,main.cpp改成:

#include <stdio.h>

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 <stdio.h>
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代碼只會(huì)以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 <stdio.h>

#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、注意事項(xiàng)

  • C++編譯器不能以c的方式編譯重載函數(shù)

  • 編譯方式?jīng)Q定函數(shù)名被編譯后的目標(biāo)名

  • c++編譯方式將函數(shù)名和參數(shù)列表編譯成目標(biāo)名,這里舉個(gè)例子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

 

說(shuō)明ii表示兩個(gè)參數(shù),iii表示三個(gè)參數(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)名起沖突所以報(bào)錯(cuò)。

5、小結(jié):

  • 函數(shù)重載是c++對(duì)c的一個(gè)重要升級(jí)

  • 函數(shù)重載通過(guò)參數(shù)列表區(qū)分不同的同名函數(shù)

  • extern關(guān)鍵字能夠?qū)崿F(xiàn)c和c++的相互調(diào)用

  • 編譯方式?jīng)Q定符號(hào)表中的函數(shù)名的最終目標(biāo)名

本文參與“OSC源創(chuàng)計(jì)劃”,歡迎正在閱讀的你也加入,一起分享。

看完上述內(nèi)容,你們對(duì)c++中怎么實(shí)現(xiàn)重載函數(shù)有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。

網(wǎng)頁(yè)標(biāo)題:c++中怎么實(shí)現(xiàn)重載函數(shù)
轉(zhuǎn)載源于:http://chinadenli.net/article14/ppgpge.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供虛擬主機(jī)網(wǎng)站收錄、網(wǎng)站改版、網(wǎng)站營(yíng)銷、App開(kāi)發(fā)網(wǎng)頁(yè)設(shè)計(jì)公司

廣告

聲明:本網(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)

成都app開(kāi)發(fā)公司