在C++中,可以為參數(shù)指定默認(rèn)值,C語(yǔ)言是不支持默認(rèn)參數(shù)的,Java也不支持?。?!
我們提供的服務(wù)有:網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作、微信公眾號(hào)開(kāi)發(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)值只能賦值一次,或者是在聲明中,或者是在定義中,如下所示
- /*正確*/
- #include <iostream>
- int f(int a=5);
- int f(int a)
- {
- std::cout <<a<<std::endl;
- return a;
- }
- int main()
- {
- f();
- return 0;
- }
- /*正確*/
- #include <iostream>
- int f(int a=5)
- {
- std::cout <<a<<std::endl;
- return a;
- }
- int main()
- {
- f();
- return 0;
- }
- /*正確*/
- #include <iostream>
- int f(int a);
- int f(int a=5)
- {
- std::cout <<a<<std::endl;
- return a;
- }
- int main()
- {
- f();
- return 0;
- }
- /*錯(cuò)誤*/
- #include <iostream>
- int f(int a=5);
- int f(int a=5)
- {
- std::cout <<a<<std::endl;
- return a;
- }
- int main()
- {
- f();
- return 0;
- }
- [niuxinli@localhost ~]$ make test
- g++ test.cpp -o test
- test.cpp: In function ‘int f(int)’:
- test.cpp:3: error: default argument given for parameter 1 of ‘int f(int)’
- test.cpp:2: error: after previous specification in ‘int f(int)’
- 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è)限定。
- #include <iostream>
- int f(int a,int b);
- int f(int a=5,int b)
- {
- std::cout <<a<<std::endl;
- return a;
- }
- int main()
- {
- f(6);
- return 0;
- }
- g++ test.cpp -o test
- test.cpp: In function ‘int f(int, int)’:
- test.cpp:3: error: default argument missing for parameter 2 of ‘int f(int, int)’
- 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)閍沒(méi)有默認(rèn)值
mal(3, , 9) //錯(cuò)誤,應(yīng)按從左到右順序逐個(gè)調(diào)用
(4)默認(rèn)參數(shù)可以是全局變量,全局常量,還可以是函數(shù),但是不能是局部變量,因?yàn)榫植孔兞吭诰幾g時(shí)未定
如
- [niuxinli@localhost ~]$ cat test.cpp
- #include <iostream>
- int x = 5;
- int f(int a,int b,int c);
- int f(int a,int b=5,int c=x)
- {
- std::cout <<a+b+c<<std::endl;
- return a;
- }
- int f2(int (*func)(int,int,int)=f )
- {
- func(2,3,5);
- return 0;
- }
- int main()
- {
- f(1);
- f2();
- return 0;
- }
- [niuxinli@localhost ~]$ make test && ./test
- g++ test.cpp -o test
- 11
- 10
但是注意一點(diǎn),func不能使用默認(rèn)參數(shù)了,因?yàn)閒unc是局部變量,它是后來(lái)被賦值成f的
- [niuxinli@localhost ~]$ cat test.cpp
- #include <iostream>
- int x = 5;
- int f(int a,int b,int c);
- int f(int a,int b=5,int c=x)
- {
- std::cout <<a+b+c<<std::endl;
- return a;
- }
- int f2(int (*func)(int,int,int)=f )
- {
- func(2);
- return 0;
- }
- int main()
- {
- f(1);
- f2();
- return 0;
- }
- [niuxinli@localhost ~]$ make test
- g++ test.cpp -o test
- test.cpp: In function ‘int f2(int (*)(int, int, int))’:
- test.cpp:11: error: too few arguments to function
- make: *** [test] Error 1
文章標(biāo)題:21天學(xué)通C++(1-1)
文章網(wǎng)址:http://jinyejixie.com/article14/gceoge.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google、營(yíng)銷型網(wǎng)站建設(shè)、虛擬主機(jī)、定制開(kāi)發(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)