First let’s clarify the difference between overloading and rewriting:
1. Overloading generally refers to two different functions in the same scope using the same name, but they must have different parameter lists, such as different parameter types or different number of parameters.
2. Override refers to reimplementing the virtual function in the base class in the subclass (remember, it must be a virtual function! If the subclass reimplements a non-virtual function in the base class, It should be called "hide"!).
Now let’s get it straight: Suppose you overload the operator “+”. In fact, the function name you overload is “+”, which has the same function name as the original (built-in) “+”. But your overloaded "+" and the original "+" should have different parameter lists (otherwise you wouldn't need to overload). In fact, C++ requires that when overloading an operator, you must ensure that at least one parameter is your custom class type. If you overload it like this
int operator+(int a, int b);
The compiler will report an error. You will find that the whole process seems to have nothing to do with virtual functions. Therefore, operator overloading can only be called "overloading", not "rewriting".
override: function override (overwriting is too easy to misunderstand)
overload: overload
operator overload:Operator overload
The reason why it is not called overwriting is because this feature has nothing to do with inheritance. Overloading focuses more on its extended nature.
First let’s clarify the difference between overloading and rewriting:
1. Overloading generally refers to two different functions in the same scope using the same name, but they must have different parameter lists, such as different parameter types or different number of parameters.
2. Override refers to reimplementing the virtual function in the base class in the subclass (remember, it must be a virtual function! If the subclass reimplements a non-virtual function in the base class, It should be called "hide"!).
Now let’s get it straight: Suppose you overload the operator “+”. In fact, the function name you overload is “+”, which has the same function name as the original (built-in) “+”. But your overloaded "+" and the original "+" should have different parameter lists (otherwise you wouldn't need to overload). In fact, C++ requires that when overloading an operator, you must ensure that at least one parameter is your custom class type. If you overload it like this
The compiler will report an error. You will find that the whole process seems to have nothing to do with virtual functions. Therefore, operator overloading can only be called "overloading", not "rewriting".
You think of an operator as a function, but in fact it is a function. Personal opinion
Operator overloading is a concept as a whole. Why do you have to take it apart and look at it?
Corresponds to the original text:
The reason why it is not called overwriting is because this feature has nothing to do with inheritance. Overloading focuses more on its extended nature.
Similar to function overloading. . .