C ++ collection-function Overloading
C ++ collection-function Overloading
Preface
Function overloading does not seem to be difficult, but there are still some issues worth noting. The following describes several keywords:
Body 1. Scope
Several facts need to be pointed out about the scope
Areas enclosed in braces {} are in the same scope. Common areas include function bodies, for, and if statements. Variables with the same name cannot appear in the same scope. If a function has the same name, it is a function overload problem. Whether or not the same name is in different scopes does not affect. Regions outside of all functions are global scopes. First, you must note that,
Function in the same scope will be overloaded.. Functions in different scopes, even with the same name, have the same parameter type and return value type. They are also two different functions.
2. Hide when the scope is nested.
# Include
Using namespace std; // varint var = 0 in the global scope; void print (void) {cout <"Global print" <endl;} int main () {// g_var in the local scope will hide g_varint var = 1 in the global scope; // local print will also hide global printauto print = [] (void) {cout <"local print" <endl ;}; // all the following calls are local cout <"var =" <var <endl; print (); // call the global cout <": var =" <: var <endl;: print (); cin. get (); return 0 ;}
Run kernel/K/kernel/nS1LX308O1xL7Nyse + 1rK/tcShowrS/rLYz9bP87Htw/kernel/K1xM2sw/Rack + Cgo8aDM + rack/Rack = "brush: java; "> # include Using namespace std; void print (const char * str) {cout <"void print (const char * str) call" <endl; cout <str ;} void print (char * str) {cout <"void print (char * str) call" <endl; cout <str;} int main () {char str [] = "David"; print ("zhangxiang"); cout <endl; print (str); cin. get (); return 0 ;}Run
If you change void print (const char * str) to void print (char * const str), the following error occurs: error C2084: function "void print (char * const) after compilation) 'Already has a body, that is, the top-level const cannot distinguish between overload.
3. Overwrite or overwrite issues exist only for virtual functions. Overwriting is similar to hiding, but not overloading. Coverage is discussed in virtual functions.
The C ++ directory of this column picks up the CCPP Blog directory of all contents