21天學(xué)通C++(1-1)

函數(shù)默認(rèn)參數(shù)

在C++中,可以為參數(shù)指定默認(rèn)值,C語(yǔ)言是不支持默認(rèn)參數(shù)的,Java也不支持?。?!

我們提供的服務(wù)有:網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作、微信公眾號(hào)開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、達(dá)日ssl等。為上1000家企事業(yè)單位解決了網(wǎng)站和推廣的問(wèn)題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的達(dá)日網(wǎng)站制作公司

默認(rèn)參數(shù)的語(yǔ)法與使用:
(1)在函數(shù)聲明或定義時(shí),直接對(duì)參數(shù)賦值。這就是默認(rèn)參數(shù);
(2)在函數(shù)調(diào)用時(shí),省略部分或全部參數(shù)。這時(shí)可以用默認(rèn)參數(shù)來(lái)代替。

注意事項(xiàng):

(1)函數(shù)默認(rèn)值只能賦值一次,或者是在聲明中,或者是在定義中,如下所示

  1. /*正確*/  
  2. #include <iostream> 
  3. int f(int a=5);  
  4. int f(int a)  
  5. {  
  6.         std::cout <<a<<std::endl;  
  7.         return a;  
  8. }  
  9. int main()  
  10. {  
  11.         f();  
  12.         return 0;  
  13. }  
  14.  
  15. /*正確*/  
  16. #include <iostream> 
  17. int f(int a=5)  
  18. {  
  19.         std::cout <<a<<std::endl;  
  20.         return a;  
  21. }  
  22. int main()  
  23. {  
  24.         f();  
  25.         return 0;  
  26. }  
  27.  
  28. /*正確*/  
  29. #include <iostream> 
  30. int f(int a);  
  31. int f(int a=5)  
  32. {  
  33.         std::cout <<a<<std::endl;  
  34.         return a;  
  35. }  
  36. int main()  
  37. {  
  38.         f();  
  39.         return 0;  
  40. }  
  41.  
  42.  
  43. /*錯(cuò)誤*/  
  44. #include <iostream> 
  45. int f(int a=5);  
  46. int f(int a=5)  
  47. {  
  48.         std::cout <<a<<std::endl;  
  49.         return a;  
  50. }  
  51. int main()  
  52. {  
  53.         f();  
  54.         return 0;  
  55. }  
  56.  
  57. [niuxinli@localhost ~]$ make test  
  58. g++     test.cpp   -o test  
  59. test.cpp: In function ‘int f(int)’:  
  60. test.cpp:3: error: default argument given for parameter 1 of ‘int f(int)’  
  61. test.cpp:2: error: after previous specification in ‘int f(int)’  
  62. make: *** [test] Error 1  

(2) 默認(rèn)參數(shù)定義的順序?yàn)樽杂业阶蟆<慈绻粋€(gè)參數(shù)設(shè)定了缺省值時(shí),其右邊的參數(shù)都要有缺省值。比如int f(int a, int b=1,int c=2,int d=3)是對(duì)的,但是int f(int a,int b=1,int c=2,int d)就是錯(cuò)的。這個(gè)的原因很顯然,你傳幾個(gè)參數(shù),編譯器都認(rèn)為是從左向右的,比如int f(int a,int b=1,int c),傳入了f(1,2),它會(huì)認(rèn)為a=1,b=2,那c呢?所以必須做這個(gè)限定。

  1. #include <iostream> 
  2. int f(int a,int b);  
  3. int f(int a=5,int b)  
  4. {  
  5.         std::cout <<a<<std::endl;  
  6.         return a;  
  7. }  
  8. int main()  
  9. {  
  10.         f(6);  
  11.         return 0;  
  12. }  
  13.  
  14. g++     test.cpp   -o test  
  15. test.cpp: In function ‘int f(int, int)’:  
  16. test.cpp:3: error: default argument missing for parameter 2 of ‘int f(int, int)’  
  17. make: *** [test] Error 1  

(3)默認(rèn)參數(shù)調(diào)用時(shí),則遵循參數(shù)調(diào)用順序,自左到右逐個(gè)調(diào)用。這一點(diǎn)要與第(2)分清楚,不要混淆。

如:void mal(int a, int b=3, int c=5); //默認(rèn)參數(shù)
mal(3, 8, 9 ); //調(diào)用時(shí)有指定參數(shù),則不使用默認(rèn)參數(shù)
mal(3, 5); //調(diào)用時(shí)只指定兩個(gè)參數(shù),按從左到右順序調(diào)用,相當(dāng)于mal(3,5,5);
mal(5); //調(diào)用時(shí)只指定1個(gè)參數(shù),按從左到右順序調(diào)用v當(dāng)于mal(5,3,5);
mal( ); //錯(cuò)誤,因?yàn)閍沒有默認(rèn)值
mal(3, , 9) //錯(cuò)誤,應(yīng)按從左到右順序逐個(gè)調(diào)用

(4)默認(rèn)參數(shù)可以是全局變量,全局常量,還可以是函數(shù),但是不能是局部變量,因?yàn)榫植孔兞吭诰幾g時(shí)未定

  1. [niuxinli@localhost ~]$ cat test.cpp  
  2. #include <iostream> 
  3. int x = 5;  
  4. int f(int a,int b,int c);  
  5. int f(int a,int b=5,int c=x)  
  6. {  
  7.         std::cout <<a+b+c<<std::endl;  
  8.         return a;  
  9. }  
  10. int f2(int (*func)(int,int,int)=f )  
  11. {  
  12.     func(2,3,5);  
  13.     return 0;  
  14. }  
  15. int main()  
  16. {  
  17.         f(1);  
  18.         f2();  
  19.         return 0;  
  20. }  
  21.  
  22. [niuxinli@localhost ~]$ make test && ./test  
  23. g++     test.cpp   -o test  
  24. 11  
  25. 10  

但是注意一點(diǎn),func不能使用默認(rèn)參數(shù)了,因?yàn)閒unc是局部變量,它是后來(lái)被賦值成f的

  1. [niuxinli@localhost ~]$ cat test.cpp  
  2. #include <iostream> 
  3. int x = 5;  
  4. int f(int a,int b,int c);  
  5. int f(int a,int b=5,int c=x)  
  6. {  
  7.         std::cout <<a+b+c<<std::endl;  
  8.         return a;  
  9. }  
  10. int f2(int (*func)(int,int,int)=f )  
  11. {  
  12.     func(2);  
  13.     return 0;  
  14. }  
  15. int main()  
  16. {  
  17.         f(1);  
  18.         f2();  
  19.         return 0;  
  20. }  
  21. [niuxinli@localhost ~]$ make test  
  22. g++     test.cpp   -o test  
  23. test.cpp: In function ‘int f2(int (*)(int, int, int))’:  
  24. test.cpp:11: error: too few arguments to function  
  25. make: *** [test] Error 1  

文章標(biāo)題:21天學(xué)通C++(1-1)
文章網(wǎng)址:http://muchs.cn/article14/gceoge.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google營(yíng)銷型網(wǎng)站建設(shè)、虛擬主機(jī)、定制開發(fā)靜態(tài)網(wǎng)站網(wǎng)站策劃

廣告

聲明:本網(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開發(fā)公司