Turn from: http://www.cnblogs.com/qlee/archive/2011/07/04/2097055.html
Overloading, overwriting, and hiding of member functions
The overloading, overwriting (override) of member functions is easily confused with concealment, and C + + programmers have to figure out
Concept, otherwise mistakes will be impossible to guard against.
8.2.1 Overload and Overlay
Features that are overloaded by member functions:
(1) The same range (in the same class);
(2) The function name is the same;
(3) different parameters;
(4) virtual keyword is optional.
Overlay refers to a derived class function that overrides a base class function, characterized by:
(1) different ranges (in derived classes and base classes, respectively);
(2) The function name is the same;
(3) the same parameter;
(4) The base class function must have the virtual keyword.
In the example 8-2-1, Functions base::f (int) and base::f (float) are overloaded with each other, while base::g (void)
Covered by derived::g (void).
#include <iostream.h>
Class Base
{
Public
void f (int x) {cout << "base::f (int)" << x << Endl;}
void f (float x) {cout << "base::f (float)" << x << Endl;}
virtual void g (void) {cout << "base::g (void)" << Endl}
};
Class Derived:public Base
{
Public
virtual void g (void) {cout << "derived::g (void)" << Endl}
};
void Main (void)
{
Derived D;
Base *PB = &d;
Pb->f (42); Base::f (int) 42
Pb->f (3.14f); Base::f (float) 3.14
Pb->g (); Derived::g (void)
}
Example 8-2-1 a member function overload and overwrite
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 time, whether there is no virtual
keyword, the functions of the base class are hidden (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 has no virtual
Key words. 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. Due to the lack of understanding,
The occurrence of "concealment" is shadowy and often produces confusing results.
In Example 8-2-2 (b), BP and DP point to the same address, and the result should be the same, but things
This is 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.3get rid of hidden
Hiding the rules caused a lot of trouble. In the example 8-2-3 program, the statement pd->f (10) is intended to invoke the letter
Number Base::f (int), but base::f (int) is unfortunately hidden by derived::f (char *). Because of the number 10
cannot be implicitly converted to a string, so 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:
1. A person who writes a 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
2. If the class derived has multiple base classes (multiple inheritance), it is sometimes unclear which base classes define the function F. If there are no hidden rules, then pd->f (10) may invoke an unexpected base class function F. Although hiding the rules doesn't seem to make sense, it does kill the accidents.