C++之函数重载
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é)
- 上一篇: 管理员账户遇到“操作需要管理员权限”解决
- 下一篇: MVC与三层架构