The concepts of overload, overwrite, and override are easy to confuse, and the Chinese translations of overwrite and override are varied, making people very confuse:
Overload
In a C ++ program, several functions with similar semantics and functions can be represented by the same name, but different parameters (including different types and sequences), that isFunction overload.
(1) the same range (in the same class );
(2) The function name is the same;
(3) parameters are different;
Please note that reload Parsing is in progressReturn type not consideredAnd the declared functions in different scopes are not considered to be overloaded.
Override Overwrite
A function of a derived class overwrites a 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.
Overwrite: rewrite
The function of the derived class shields the base class functions with the same name. 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. In this case, functions of the base class will be hidden no matter whether there is any virtual keyword.
(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)
Override means that multiple function prototypes are identical in different scopes, but different functions are implemented. In C ++, classes are often inherited. When a method in the base class is a virtual or pure virtual function (of course, the access permission must be public or protected, because the private function is not virtual in C ++ design, the re-implementation of this method in its subclass is override. When using the function, you can use the pointer of the base class or reference the specific object to determine which method to call, so as to realize the function polymorphism. For non-virtual member functions in the base class, if the child class also declares a function with the same name as the function, the function in the base class (maybe a series of functions, if this function is overloaded in the base class, it will be hidden and can be called through the domain parsing operator. However, according to the C ++ design philosophy, non-virtual member functions in the base class do not need to be modified in the subclass, so if this type of hiding occurs in the subclass, it means you should change the function in the base class to the virtual type, and then override, haha! Overload means that in the same scope, multiple functions have the same name, but the number and type of parameters are different (of course, the same number and type, if the order is different, haha), because the function overload mechanism is that the function signature in C ++ is related to its parameters, unlike in C, it is only related to the function name.
In short, the biggest difference between override and overload is that the scope is different and whether the function prototype is the same.
Overload overwrite hiding
The function of the derived class is exactly the same as the function of the base class (the function name and parameter list are the same), but the base class function does not have the virrual keyword. In some cases, the base class function will be hidden rather than overwritten.
The function of the derived class has the same name as the function of the base class, but the parameter list is different. In this case, functions of the base class will be hidden no matter whether the base class function declaration has the virrual keyword. note that this is different from the overload. The overload occurs in the same class,