Yes, I do understand the difference between them. What I want to know is: why OVERRIDE a method? What is the good in doing it?
In case of overload: the only advantage is you haven't to think in different names to functions?
C++ – Override and overload in C++
c++overloadingoverriding
Related Question
- C# – Why is it important to override GetHashCode when Equals method is overridden
- C++ – The Definitive C++ Book Guide and List
- Javascript – Function overloading in Javascript – Best practices
- Java – ‘Must Override a Superclass Method’ Errors after importing a project into Eclipse
- C++ – Why do we need virtual functions in C++
- C++11 introduced a standardized memory model. What does it mean? And how is it going to affect C++ programming
- C++ – the difference between ‘typedef’ and ‘using’ in C++11
- C++ – Replacing a 32-bit loop counter with 64-bit introduces crazy performance deviations with _mm_popcnt_u64 on Intel CPUs
Best Solution
Overloading generally means that you have two or more functions in the same scope having the same name. The function that better matches the arguments when a call is made wins and is called. Important to note, as opposed to calling a virtual function, is that the function that's called is selected at compile time. It all depends on the static type of the argument. If you have an overload for
B
and one forD
, and the argument is a reference toB
, but it really points to aD
object, then the overload forB
is chosen in C++. That's called static dispatch as opposed to dynamic dispatch. You overload if you want to do the same as another function having the same name, but you want to do that for another argument type. Example:they both print their argument, so they are overloaded. But the first prints a foo, and the second prints a bar. If you have two functions that do different things, it's considered bad style when they have the same name, because that can lead to confusion about what will happen actually when calling the functions. Another usecase for overloading is when you have additional parameters for functions, but they just forward control to other functions:
That can be convenient for the caller, if the options that the overloads take are often used.
Overriding is something completely different. It doesn't compete with overloading. It means that if you have a virtual function in a base class, you can write a function with the same signature in the derived class. The function in the derived class overrides the function of the base. Sample:
Now, if you have an object and call the
print
member function, the print function of the derived is always called, because it overrides the one of the base. If the functionprint
wasn't virtual, then the function in the derived wouldn't override the base function, but would merely hide it. Overriding can be useful if you have a function that accepts a base class, and every one that's derived from it:Now, even though at compile time the compiler only knows that b is at least base, print of the derived class will be called. That's the point of virtual functions. Without them, the print function of the base would be called, and the one in the derived class wouldn't override it.