8.2.2 's confusing hidden rules
It was not difficult to distinguish between overloads and overrides, but the hidden rules of C + + caused the complexity of the problem to increase precipitously. Here "hiding" means that a function of a derived class masks a base class function with the same name as the following:
(1) If the function of the derived class has the same name as the function of the base class, but the parameters are different. At this point, the function of the base class is hidden, regardless of the virtual keyword (Note that you are not confused with overloading).
(2) If the function of the derived class has the same name as the function of the base class, and the parameters are the same, the base class function does not have the virtual keyword. At this point, the functions of the base class are hidden (note that you are confused with overrides).
Sample program 8-2-2 (a):
(1) function derived::f (float) covers base::f (float).
(2) function derived::g (int) hides base::g (float) instead of overloading.
(3) function derived::h (float) hides base::h (float) instead of overwriting it.
#include <iostream.h>
Class Base
{
Public
virtual void F (float x) {cout << "base::f (float)" << x << Endl;}
void g (float x) {cout << "base::g (float)" << x << Endl;}
void h (float x) {cout << "base::h (float)" << x << Endl;}
};
Class Derived:public Base
{
Public
virtual void F (float x) {cout << "derived::f (float)" << x << Endl;}
void g (int x) {cout << "derived::g (int)" << x << Endl;}
void h (float x) {cout << "derived::h (float)" << x << Endl;}
};
Example 8-2-2 (a) overload, overwrite, and hide of member functions
According to the authors, many C + + programmers are unaware of the "hidden" thing. Because the understanding is not deep enough, the "hidden" occurrence is shadowy, often produces the puzzling result.
In Example 8-2-2 (b), BP and DP point to the same address, and the result should be the same, but that's not the case.
void Main (void)
{
Derived D;
Base *PB = &d;
Derived *PD = &d;
Good:behavior depends solely on type of the object
Pb->f (3.14f); Derived::f (float) 3.14
Pd->f (3.14f); Derived::f (float) 3.14
Bad:behavior depends on type of the pointer
Pb->g (3.14f); Base::g (float) 3.14
Pd->g (3.14f); Derived::g (int) 3 (surprise!)
Bad:behavior depends on type of the pointer
Pb->h (3.14f); Base::h (float) 3.14 (surprise!)
Pd->h (3.14f); Derived::h (float) 3.14
}
Example 8-2-2 (b) overload, overlay, and hide comparisons
8.2.3 Get rid of hidden
Hiding the rules caused a lot of trouble. In the example 8-2-3 program, the statement pd->f (10) was intended to invoke the function base::f (int), but base::f (int) was unfortunately hidden by derived::f (char *). Because the number 10 cannot be implicitly converted to a string, an error occurred at compile time.
Class Base
{
Public
void f (int x);
};
Class Derived:public Base
{
Public
void f (char *str);
};
void Test (void)
{
Derived *pd = new Derived;
Pd->f (10); Error
}
Example 8-2-3 error caused by shadowing
From the example 8-2-3, it seems silly to hide the rules. But there are at least two reasons for hiding rules:
U Write statement pd->f (10) may really want to invoke the Derived::f (char *) function, but he mistakenly writes the arguments incorrectly. With a hidden rule, the compiler can clearly point out the error, which is not necessarily a good thing. Otherwise, the compiler will silently mistake, the programmer will be very difficult to find this error, shed the curse.
U if a class derived has multiple base classes (multiple inheritance), sometimes it is unclear which base class defines the function F. If there are no hidden rules, then pd->f (10) may invoke an unexpected base class function F. Although the hidden rules do not seem to make sense, it does eliminate these surprises.
In the example 8-2-3, if the statement pd->f (10) must call the function base::f (int), then modify the class derived to read as follows.
Class Derived:public Base
{
Public
void f (char *str);
void f (int x) {base::f (x);}
};