Overloading & Overriding & Hiding

(The following <same function parameters> means that the number of parameters, parameter types and return types are the same)

1. Overload:

Features: the same function name, different function parameters, must be located in the same domain (class);

Second, cover (rewrite) (override):

Features: same function name, same function parameters, respectively located in derived class and base class, virtual (virtual function) ;

Three, hide (hide):

feature:

The function name is the same, the parameters are different, and they are located in the derived class and the base class respectively, regardless of whether the function with the same name in the parent class contains the virtual keyword (that is, the difference from overloading is whether the two functions are in the same domain (class) ) ;

The function name is the same and the parameters are the same, but the base class function does not have the virtual keyword . At this time, the base class function is hidden (be careful not to confuse it with coverage);

That is: the function in the derived class hides (shields) the function of the same name in the base class.

Regarding the understanding of hiding, when calling a member function of a class, the compiler will look up the definition of the function step by step along the inheritance chain of the class, and stop searching if it finds it; so if a derived class and a base class have a function with the same name (regardless of whether the function parameters are the same), and the compiler finally chooses the function in the derived class, then it is said that the member function of the derived class "hides" the function of the same name in the base class, that is, it prevents the compiler from continuing to move upwards Find the definition of a function. (So ​​functions with the same name, although the function parameters are different, but when they are located in the derived class and the base class, the base class function will be blocked.)

Guess you like

Origin blog.csdn.net/SFDWU3QVG/article/details/126875566