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

歡迎訪問(wèn) 生活随笔!

生活随笔

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

c/c++

C++之函数重载

發(fā)布時(shí)間:2025/3/21 c/c++ 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++之函数重载 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>

C++的重載,C++的重載的兩個(gè)函數(shù)參數(shù)數(shù)量可以相同也可以不同,當(dāng)參數(shù)數(shù)量相同時(shí),只需要對(duì)應(yīng)參數(shù)類型不同即稱為重載。

In some programming languages, function overloading or method overloading is the ability to create multiple methods of the same name with different implementations. Calls to an overloaded function will run a specific implementation of that function appropriate to the context of the call, allowing one function call to perform different tasks depending on context.

For example, doTask() and doTask(object O) are overloaded methods. To call the latter, an object must be passed as a parameter, whereas the former does not require a parameter, and is called with an empty parameter field. A common error would be to assign a default value to the object in the second method, which would result in an ambiguous call error, as the compiler wouldn't know which of the two methods to use.

Another appropriate example would be a Print(object O) method. In this case one might like the method to be different when printing, for example, text or pictures. The two different methods may be overloaded as Print(text_object T); Print(image_object P). If we write the overloaded print methods for all objects our program will "print", we never have to worry about the type of the object, and the correct function call again, the call is always: Print(something).

以下程序?qū)崿F(xiàn)了函數(shù)的重載。

#include?<stdlib.h> #include?<iostream> using?namespace?std; void?fun(int?i=30,?int?j=20,?int?k=10); int?main(void) {fun();fun(100);fun(100,?200);fun(100,?200,?300);system("pause");return?0;} void?fun(int?i,?int?j,?int?k) {cout?<<?i?<<","<<?j?<<","<<?k<<endl; }

程序中,同名函數(shù),函數(shù)參數(shù)的個(gè)數(shù)不同,注意形參默認(rèn)值和函數(shù)在初始化時(shí)指定的形參的覆蓋順序是從左往右一順來(lái)的。其中,形參默認(rèn)值只能對(duì)應(yīng)最后一個(gè)形參。運(yùn)行結(jié)果:

當(dāng)參數(shù)類型不一致時(shí),也可形成重載:

#include?<stdlib.h> #include?<iostream> using?namespace?std; void?fun(int?i=30,?int?j=20,?int?k=10);//inline?void?fun(int?i=30,?int?j=20,?int?k=10); void?fun(double?i,?double?j);//inline?void?fun(double?i,?double?j);int?main(void) {fun(1.1,?2.2);fun(1,?2);system("pause");return?0;}void?fun(int?i,?int?j,?int?k) {cout?<<?i?<<","<<?j?<<","<<?k<<endl; } void?fun(double?i,?double?j) {cout?<<?i?<<?","?<<?j?<<?endl; }

運(yùn)行結(jié)果:

The intent of the inline keyword is to serve as an indicator to the optimizer that inline substitution of the function is preferred over function call, that is, instead of executing the call CPU instruction to transfer control to the function body, a copy of the function body is executed without generating the call. This avoids extra overhead created by the function call (copying the arguments and retrieving the result) but it may result in a larger executable as the code for the function has to be repeated multiple times.

inline(內(nèi)聯(lián))是對(duì)編譯器編譯時(shí)的一種建議,不影響結(jié)果,只改變了編譯的效率。減小了直接調(diào)用函數(shù)時(shí)造成的開銷。不過(guò)最終生成的可執(zhí)行目標(biāo)文件可能會(huì)比之前大。

轉(zhuǎn)載于:https://my.oschina.net/donngchao/blog/527497

《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀

總結(jié)

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

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