Compared with C functions, C ++ addsOverload, inline, const, and virtual
Four new mechanisms. The overload and inline mechanisms can be used for both global functions and class member functions,Const and
The virtual mechanism is only used for member functions of the class.
1. overloaded)
Reload reason:
1: In C ++ProgramYou can use the same name to represent functions with similar semantics and functions, that is, function overloading.
This makes it easier to remember and improves the ease of use of functions.
2: The class constructor requires an overload mechanism. C ++ requires that the constructor and the class have the same name and can have only one name. What should I do if I want to create an object using several different methods? There is no choice but to use the overload mechanism. Therefore, a class can have multiple constructors with the same name.
Implementation of heavy load:
How are they differentiated? We naturally think of two elements of function interfaces:Parameters and return values.
If the parameters of functions with the same name are different (including different types and order), they are different functions.
If functions with the same name only have different return value types, they can be distinguished, but sometimes they cannot. For example:
Void function (void );
Int function (void );
The first function has no return value, and the second function has the int type. If you call a function like this:
Int x = function ();
Then we can determine that the function is the second function.
Void eat (beef ...);
Void eat (fish ...);
Void eat (chicken ...);
The problem is that in C ++/C Programs, we can ignore the return value of the function. In this case, neither the compiler nor the programmer knows which function is called.Therefore, you can only distinguish between overloaded functions by parameters rather than different return value types.. The compiler generates different internal identifiers for each overload function based on parameters. For example, the compiler generates internal identifiers such as _ eat_beef, _ eat_fish, and _ eat_chicken for the three eat functions in the preceding example (different compilers may generate internal identifiers of different styles ).
What if a C ++ program needs to call a compiled C function?
Assume that the declaration of a C function is as follows:
Void Foo (int x, int y );
After the function is compiled by the C compiler, its name in the library is _ Foo, while the C ++ compiler generates names such as _ foo_int_int to support function overloading and secure type connection. Because the compiled name is different, C ++ programs cannot directly call C functions. C ++ provides a C connection to exchange the specified symbol extern "C" to solve this problem.
For example:
Extern "C"
{
Void Foo (int x, int y );
Other // other functions
}
Or write it
Extern "C"
{
# Include "myheader. h"
Other // other C header files
}
This tells C ++ to compile the interpreter. Function foo is a C connection and should find the name _ Foo in the library instead of _ foo_int_int.. The C ++ compiler developer has processed the header files of the C standard library as extern "C", so we can use # include to directly reference these header files.
Note: Not two functions with the same name can constitute an overload. The same name of a global function and a member function of a class is not considered an overload because the function has different scopes.. For example:
Void print (...); // global function
Class
{...
Void print (...); // member function
}
Whether the parameters of the two print functions are different, if a member function of the class calls the global function print, the ':' flag should be added when the global function is called to distinguish it from the member function print. For example
: Print (...); // indicates that print is a global function rather than a member function.
Note: Be careful when implicit type conversion leads to ambiguity of the overloaded function.
The first output function parameter is of the int type, and the second output function parameter is of the float type. Because the number itself does not have a type, it is automatically converted (called implicit type conversion) when it is treated as a parameter ). Statement output (0.5) will generate a compilation error because the compiler does not know whether to convert 0.5 to an int or float type parameter. Implicit type conversion can simplify program writing in many places, but it may leave hidden risks.
# Include <iostream. h>
Void output (int x); // function declaration
Void output (float X); // function declaration
Void output (int x)
{
Cout <"output int" <x <Endl;
}
Void output (float X)
{
Cout <"output float" <x <Endl;
}
Void main (void)
{
Int x = 1;
Float y = 1.0;
Output (x); // output int 1
Output (y); // output float 1
Output (1); // output int 1
// Output (0.5 );//Error! Ambiguous call, because of automatic type conversion
Output (INT (0.5); // output int 0
Output (float (0.5); // output float 0.5
}
Implicit type conversion leads to ambiguity in overloaded functions
From: http://blog.csdn.net/sendy888/article/details/1738997