1. Override refers to the function of a derived class that overwrites the base class function. The features are as follows:
(1) different scopes (located in the derived class and the base class respectively );
(2) The function name is the same;
(3) The parameters are the same;
(4) basic functions must have virtual keywords.
A bit: It's much safer and everything is under control.
2. "hide" means that the function of the derived class shields the base class function with the same name as it. The rules are as follows:
(1) If the function of the derived class has the same name as the function of the base class, but the parameter is different. At this time, no matter whether there is a virtual keyword
Functions of the base class will be hidden (Be careful not to be confused with overload ).
(2) If the function of the derived class has the same name and parameter as the function of the base class, but the base class function does not have the virtual keyword. At this time, the base class functions are hidden (Be sure not to confuse with overwrite)
Disadvantages: The base class refers to the fact that the pointer of a derived class, except the virtual function, calls itself, including hidden. The hidden function called by the pointer of a derived class does not appear in the base class function. The disadvantage of hiding is that base class pointers call functions of the same name, but actually call itself.
The derived subclass overwrites a method of the parent class, and the parameters of the parent class method are changed. The Compiler did not find this problem during compilation. When the program is running, an endless loop occurs without any error message. This bug was confusing and took a lot of time for colleagues. It was finally discovered that the parent class method had changed, and the override method in the subclass was not updated. Hidden methods may be called unexpectedly to obtain unexpected results ..
Hidden functions:
Class B
{
Foo (float );
Foo (double );
}
Class D
{
Foo (INT );
}
Int main ()
{
B * pb = new D ();
Pb-> Foo (10.5 );
}
Which function does it want to call? If no concealment is used, it will call the Foo (double) of the base class, and you may want to call the Foo (INT) of the derived class in implementation ). Therefore, C ++ simply uses the name to be concealed and clearer.
Summary example:
# 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 ;}
};
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
}
(1) The derived: F (float) function overwrites base: F (float ).
(2) The derived: G (INT) function hides the base: G (float) instead of the overload.
(3) The derived: H (float) function hides base: H (float) instead of overwrite.
The occurrence of a hidden event can be described as a ghost and often produces confusing results.
Hidden overload is easy to cause, and an additional function is inadvertently added, and may be called.
Therefore, it is not recommended to use hide. It is best to use overwrite.
//////////////////////////////////////// /// // The Editor's note:
When I was a newbie, I had to summarize and ask experts a little bit, so many of them were pieced together and some were blunt.
Learn from each other. Thanks to those who have helped me!