The difference between overloading, covering and hiding

The detailed explanation of overloading, overwriting and hiding is Google;
(The following <function parameter is the same> refers to the number of parameters, parameter type and return type are the same)
First, overload (overload):
features: the same function name, function parameters Different, must be in the same domain (class);
Second, override:
Features: The function name is the same, the function parameter is the same, are located in the derived class and the base class, virtual (virtual function);
Three, hide (hide) :
Namely: The function in the derived class hides (shields) the function with the same name in the base class.
Case 1: The function name is the same, the function parameter is the same, and they are located in the derived and base classes, respectively, virtual – for coverage;
Case 2: The function name is the same, the function parameters are the same, and they are in the derived and base classes, respectively – it is hidden; (ie The difference from the coverage is whether the function in the base class is a virtual function)
Case 3: The function name is the same, the function parameter is different, and they are located in the derived class and the base class-for hiding; (that is, the difference from overloading is whether the two functions are in In the same domain (class))
about the hidden understanding, 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, if it is found, it will stop searching; so if a derivative Both the class and a base class have a function of the same name (regardless of whether the function parameters are the same), and the compiler finally selects the function in the derived class, then the member function of this derived class "hides" the function of the same name of the base class, That is, it prevents the compiler from continuing to look up the definition of the function. (So ​​for the above case 3, the function of the same name, although the function parameters are different, but it is located in the derived class and the base class, the base class function will shield the aesthetic point of view.) Reference link: link interface
One additional point: about the hidden case 2, it is equivalent to redefining the non-virtual function in the base class, which is actually not good. For details, please refer to Article 36 in "Effective C ++": Never redefine the inherited non-virtual function.

Published 9 original articles · liked 0 · visits 253

Guess you like

Origin blog.csdn.net/a_465240/article/details/105194074