日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

C++函数高级

發(fā)布時(shí)間:2025/4/5 c/c++ 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++函数高级 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
  • 默認(rèn)參數(shù)
    注意事項(xiàng):
    (1)如果某個(gè)位置已經(jīng)有了默認(rèn)參數(shù),那么從這個(gè)位置往后,從左往右都必須有默認(rèn)值
  • int func(int a,int b = 10,int c = 20){return a + b + c; } //一旦函數(shù)的某個(gè)參數(shù)有了默認(rèn)值,那么該參數(shù)的后面的參數(shù)也必須給它們默認(rèn)值

    (2)如果函數(shù)的聲明有了默認(rèn)參數(shù),那么函數(shù)的實(shí)現(xiàn)就不能有默認(rèn)參數(shù)了。
    聲明和實(shí)現(xiàn)只能有一個(gè)有默認(rèn)參數(shù)。不然有二義性。

    #include <iostream> using namespace std;int func2(int a = 10, int b = 10); int func2(int a = 10, int b = 10){return a+b; } int main(){ cout << func2(10,10) << endl;return 0; }

    結(jié)果:

  • 占位參數(shù)
    語法:
  • void func(int a , int){}

    example:

    #include <iostream> using namespace std;void func(int a, int){cout << "this is a function!" << endl; } int main(){ func(10,10);return 0; }

    注意:這里調(diào)用func必須有兩個(gè)整形參數(shù)
    strange:占位參數(shù)在函數(shù)體里內(nèi)用不到為啥還要占位
    占位參數(shù)也可以有默認(rèn)參數(shù)

    void func(int a, int = 10){}
  • 函數(shù)重載
    作用:函數(shù)名可以相同,提高復(fù)用性
    函數(shù)重載的滿足條件:
    • 同一作用域下
    • 函數(shù)名稱相同
    • 函數(shù)的參數(shù)類型不同個(gè)數(shù)不同順序不同
      注意:函數(shù)的返回值不可以作為函數(shù)重載的條件

    注意事項(xiàng):
    (1)引用作為重載條件
    const int引用和int引用是不同的參數(shù)類型

    #include <iostream> using namespace std;void func(int &a){ //int &a=10; 不合法的 cout << "func(int &a)的調(diào)用" << endl; } void func(const int &a){ //const int &a = 10;cout << "const func(int &a)的調(diào)用" << endl; } int main(){ int a = 10; //a是變量,可讀可寫 func(a);func(10); return 0; }

    (2)函數(shù)重載碰到函數(shù)默認(rèn)參數(shù)

    #include <iostream> using namespace std;void func2(int a,int b = 10){cout << "func2(int a,int b)的調(diào)用" << endl; } void func2(int a){cout << "func2(int a)的調(diào)用" << endl; } int main(){ func2(10);//函數(shù)重載碰到默認(rèn)參數(shù)會(huì)出現(xiàn)二義性 }

    報(bào)錯(cuò)!!!編譯器不知道調(diào)用哪個(gè)!!!
    函數(shù)重載碰到默認(rèn)參數(shù)會(huì)出現(xiàn)二義性!!!!盡量避免這種情況!!!

    總結(jié)

    以上是生活随笔為你收集整理的C++函数高级的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。