function overloading
If several functions within the same scope have the same names but different parameter lists, we call them overloaded functions.
voidPrintConst Char*CP);voidPrintConst int*beg,Const int*end);voidPrintConst intia[], size_t size);intj[2] = {0,1};p rint ("Hello world!");p Rint (J, End (j)-begin (j));p Rint (Begin (j), End (j));
The main function cannot be overloaded
For overloaded functions, they should differ in the number of formal parameters or parameter types.
No two functions are allowed except for the return type, all of which have the same features.
Sometimes two formal parameter lists look different and are actually the same
int Calc (constint &a); int Calc (constint&);
Formal parameter names just play a role in helping to remember. Does it not affect the contents of the formal parameter list
Overloads and Const parameters
The top-level const does not affect the object passed into the function. A formal parameter with a top-level const cannot be separated from another formal parameter that has no top-level const
Record Lookup (Phone); Record Lookup (const Phone); Record Lookup (Phone*); Record lookup (Phoneconst);
Duplicate declarations for the above functions
If a parameter is a pointer or reference of a certain type, the function overload can be implemented by distinguishing whether it points to a constant object or a very heavy object, at which time the const is the underlying
Record lookup (account&); // reference to function scope account Record lookup (const account&); // function on a constant reference Record lookup (account*); // action to point to account pointer Record lookup (const account*); // pointer to a constant
We can only pass the const object to the const parameter,
Conversely, a very mass can be converted to a const
So the above 4 functions can be used for a very important object or a pointer to a very important object.
When we pass a very mass object or pointer to a very high-volume object, the compiler takes precedence over the very version of the function.
Const_cast and overloading
Compares the length of two string objects and returns the shorter reference
Const string &shorterstring (conststringconststring &s2) { return s1.size () < S2.size ()? s1:s2;}
We can call this function on two very-good string arguments, but the result returned is a reference to the const string.
So we need a new function, and when the argument is not a constant, the resulting result is a normal reference. Can be done using const_cast.
string &shorterstring (stringstring &s2) { &r = shorterstring ( const_cast<conststring> (S1), const_cast<conststring>(s2)); return const_cast<string&>(r);}
First, the argument is cast to a const reference, then the const version of Shorterstring is called, and the const version returns a reference to the const string and converts it to a normal string&.
Overloads and Scopes
In general, it is not a wise choice to place a function declaration in a local scope.
The general nature of the overload to the scope has not changed, and if we declare the name in the inner scope, it hides the entity with the same name declared in the outer scope. Function names cannot be overloaded in different scopes
When a function is called in scope, the compiler first looks for a declaration of the function name, and once the desired name is found within that scope, the compiler automatically ignores the entity with the same name in the outer scope. The rest is to check that the function call is valid.
In C + +, the name lookup is preceded by a type check
stringread ();voidPrintConst string&);voidPrintDouble);voidFooBar (intival) { BOOLRead =false; //new scope, hides the outer read function strings =read (); //error, read is a bool type variable voidPrintint); //declaring a function within a scopePrint"Value:"); //error, only print (int) in scope, outer print is hiddenprint (ival); //correct, call the current print (int)Print3.14); //correct, the current print (int) is called.}
voidPrintConst string&);voidPrintDouble);voidPrintint);voidFOOBAR2 (intival) {Print ("Value:"); //Overloaded Callsprint (ival); //Overloaded CallsPrint3.14); //overload call, void Print (double)}
voidPrintConst string&);voidPrintDouble);voidPrintint);voidFOOBAR2 (intival) {Print ("Value:"); //Overloaded Callsprint (ival); //Overloaded CallsPrint3.14); //overload call, void Print (double)}
[C + +] function overloading