- Example 1:
# Include <stdio. h>
Void F (int );
Void F (long );
Void F (char );Int main (INT argc, char * argv [])
{
Double value = 65.01;
F (value );
Return 0;
}
Void F (int)
{
Printf ("int value = % d/N", );
}
Void F (long)
{
Printf ("long value = % d/N", );
}
Void F (char)
{
Printf ("char value = % C/N", );
}
Compile the above Code with the following error:
Error c2668: "F": the call to the overload function is unclear.
In the F (value); call, because there is implicit data conversion, the value is automatically converted to the integer value 65, but Char, Int, long are all integer, the compiler does not know whether to call f (char), F (INT), or F (long). Because the program running path has a predictable and unique path, the compiler cannot help the program designer decide to use that function, so it cannot be compiled.
- Example 2:
# Include <stdio. h>
Void F (int );
Void F (long );
Void F (char );Int main (INT argc, char * argv [])
{
Double value = 65.01;
F (static_cast <int> (value ));
F (static_cast <char> (value ));
F (static_cast <long> (value ));
Return 0;
}
Void F (int)
{
Printf ("int value = % d/N", );
}
Void F (long)
{
Printf ("long value = % d/N", );
}
Void F (char)
{
Printf ("char value = % C/N", );
}
Since the above program uses static_cast for explicit type conversion, the compiler knows which function the program will call, so it can run correctly.
- Summary
If function Overloading is involved in the program, if the parameters at the same position are char, short, Int, long, unsigned short, unsigned int, unsigned long, unsigned char, this may cause ambiguity. Pay attention to the need to convert the display type when calling a function.
- Example 3
# Include <stdio. h>Class base
{
Public:
Void F (int A) {printf ("Base F: para = % d/N", );}
};
Class derived: public Base
{
Public:
Void F (long a) {printf ("derived F: para = % d/N", );}
};
Int main (INT argc, char * argv [])
{
Derived D;
Base & B = D;
D. F (1 );
B. F (1 );
Return 0;
}
Can this program be compiled and run successfully? The answer is yes, because the f function in derived is redefined, not overloaded. Because the f function is not a virtual function, when calling the f function, in the future, the function of the corresponding class will be called Based on the static type of the object.
D. F (1); because the static type of D is derived, the derived: F (long a) function is called;
B. F (1); because the static type of B is base, the base: F (int A) function will be called;
Running result:
Derived F: para = 1
Base F: para = 1
- Example 4
# Include <stdio. h>
Class base
{
Public:
Virtual void F (int A) {printf ("Base F: para = % d/N", );}
};
Class derived: public Base
{
Public:
Virtual void F (long a) {printf ("derived F: para = % d/N", );}
};Int main (INT argc, char * argv [])
{
Derived D;
Base & B = D;
D. F (1 );
B. F (1 );
Return 0;
}
Is there a problem with this program? A: No problem. It can run properly;
Because the f function is redefined in derived, the F function of base is overwritten, and the virtual function is not re-implemented, it is no problem to call the new function defined in derived;
B. F (1); since the static type of B is base, there is no doubt that base: F (int A) is called, so the running result is the same as that in the previous example:
Derived F: para = 1
Base F: para = 1
To sum up, if a virtual function is redefined in the subclass or the base class virtual function is overloaded, the virtual function of the same name inherited from the subclass will be overwritten. When a function is called, the function will be called Based on the static type of the object.
- Example 5
Int main (INT argc, char * argv [])
{
Derived D;
Base & B = D;
Double value = 87.01;
D. F (value );
B. F (value );
Return 0;
}
During compilation. F (value); error: Error c2668: "derived: F": the call to the overloaded function is not clear, and the ambiguity is generated as in the first example. because the value here does not know whether to convert to int or long;
But B. F (value); the call has no ambiguity, because there is only one f function in the base space. Although there is a warning, the value can only be converted to the int type in this place. It is determined that the Base will be called:: F (int A), so the compiler will not report an error.
Here, because F (INT) is redefined in the subclass and F (long) is overloaded in derived, there is a ambiguity here.
Do not redefine the virtual functions inherited by the base class in the subclass. As a result, the inherited virtual functions of the base class are overwritten.