PHPz2017-04-17 11:46:05
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".
高洛峰2017-04-17 11:46:05
You think of an operator as a function, but in fact it is a function. Personal opinion
大家讲道理2017-04-17 11:46:05
Operator overloading is a concept as a whole. Why do you have to take it apart and look at it?
迷茫2017-04-17 11:46:05
Corresponds to the original text:
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.