# Include
<Iostream>
# Include
<String>
Using namespace STD;
Class
{};
Class B:
Public
{};
Void F (B & B)
{
Cout <"F (B & B)" <Endl;
}
Void F (A &)
{
Cout <"F (A & A)" <Endl;
}
Int _ tmain (INT argc, _ tchar * argv [])
{
A * P1 = new B;
F (* P1); // call f (A &)
A;
B * P2 = (B *) &;
F (* P2); // call f (B & B)
/* F (* P2) Call analysis: F (B & B) is equivalent to F (B * P), F (A & A) is equivalent to F (A * P ),
F (* P2) is equivalent to F (P2), while P2 is B * type. Of course, F (B * P) is called, that is, F (B & B) is called ).
The same applies to F (* P1.
It can be seen that the parameter type should be matched, regardless of the object actually pointed to (function parameters only support the pointer type ). */
System ("pause ");
Return 0;
}
However, if there is no void F (A & A) function, that is:
Class
{};
Class B:
Public
{};
Void F (B & B)
{
Cout <"F (B & B)" <Endl;
}
Int _ tmain (INT argc, _ tchar * argv [])
{
A * P1 = new B;
F (* P1); // compilation error. A * cannot be implicitly converted to B *. F (A * P) is supposed to be called, but only F (B * P) is required, and a * cannot be implicitly converted to B *. Therefore, F (B * P) is not called ), error.
System ("pause ");
Return 0;
}