Function reconfiguration is mainly characterized by one interface and multiple calls, that is, polymorphism! Planting can be used for constructors
Sometimes several functions are needed to process some operations, which are similar. If you do not need to resend them, you must write n functions with different names. This is done in VB, this is because VB does not have a re-planting mechanism. When managing a large project, these functions are mixed with other functions, which can be confusing. Of course, attribute settings can be used after class encapsulation is used. However, a flexible function definition method can be added for function reconfiguration. C ++ uses function return types or parameter types and parameter quantities to differentiate which function should be called.
I. re-planting example:
Int func (int );
Int func (int A, int B );
Int func (int)
{
Return a *;
}
Int func (int A, int B)
{
Return a * B;
}
Void main ()
{
Int X;
X = func (10); // call int func (int A), x = 100
X = func (2, 30); // call int func (int A, int B), x = 60
}
Ii. Ambiguity in re-planting
1. Automatic type conversion in C ++
When two data types of different widths are operated, the compiler tries its best to unify the data types without data loss, such as float and double operations, if it is not explicitly specified as float, C ++ will be automatically converted to double for calculation. When an integer type and a floating point type operation are performed, the integer is first converted to a floating point number, constants without decimal points are considered as integers.
However, if overload is used, the compiler will distinguish which overload function to call based on the return type, parameter type, and parameter quantity. However, sometimes, the automatic conversion mechanism of data types and the overload mechanism will lead the compiler to a dead end, take the following example:
Float func (float );
Double func (double );
Call:
X = func (10.1); // can be called. The overload Mechanism regards 10.1 as the double type, so that the data is safer and the double type overload function is called.
X = func (10); // compilation Error
The compiler does not know whether to convert Integers to float or double. I think this can be avoided, but those who do the compiler have not considered this situation, there are many similar problems in C ++. However, due to the flexibility of C ++, you can always find a solution (you can directly write the memory by yourself, haha ).
2. When default parameters are used
Int func (int I );
Int func (int I, Int J = 1 );
Call:
X = func (); // can be called
X = func (1); // it cannot be called. The Compiler does not know whether to call a single parameter or the one with the default value.
Therefore, many compilers do not allow the use of default parameters. Function overloading can achieve the same effect.
3. Obtain the address of the reselling Function
Int func (int );
Int func (int a, int B );
Declare and obtain the function pointer
Int (* fp) (int a, int B );