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?
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
- Converting glm::lookat matrix to quaternion and ba
- What is the correct way to declare and use a FILE
Override is useful when you inherit from a base class and wish to extend or modify its functionality. Even when the object is cast as the base class, it calls your overridden function, not the base one.
Overloading is not necessary, but it sure makes life easier or more readable sometimes. Arguably it can make it worse, but that's when it should not be used. For example, you can have two functions that perform the same operation, but act on different kinds of things. For example
Divide(float, float)
should be different fromDivide(int, int)
, but they're basically the same operation. Wouldn't you rather remember one method name, "Divide", than have to remember "DivideFloat", "DivideInt", "DivideIntByFloat", and so on?You overload functions for three reasons:
To provide two (or more) functions that perform similar, closely related things, differentiated by the types and/or number of arguments it accepts. Contrived example:
To provide two (or more) ways to perform the same action. Contrived example:
To provide the ability to perform an equivalent action given two (or more) different input types. Contrived example:
In some cases it's worth arguing that a function of a different name is a better choice than an overloaded function. In the case of constructors, overloading is the only choice.
Overriding a function is entirely different, and serves an entirely different purpose. Function overriding is how polymorphism works in C++. You override a function to change the behavior of that function in a derived class. In this way, a base class provides interface, and the derived class provides implementation.
One use of overloading is for use in templates. In templates, you write code that can be used on different data types, and call it with different types. If functions that take different arguments had to be named differently, the code for different data types would in general have to be different, and templates just wouldn't work.
While you may not be writing templates yet, you're almost certainly using some of them. Streams are templates, and so are vectors. Without overloading, and therefore without templates, you'd need to call Unicode streams something different from ASCII streams, and you'd have to use arrays and pointers instead of vectors.
The textbook example is class Animal with method speak(). The Dog subclass overrides speak() to "bark" while the Cat subclass overrides speak() to "meow".
People already defined both overloading and overriding, so I won't elaborate.
1. You don't have to think in several names
And this is already a mighty advantage, isn't it?
Let's compare with known C API functions, and their fictional C++ variants:
This means two things: One, you must tell the compiler the type of the data it will feed to the function by choosing the right function. Two, if you want to extend it, you'll need to find fancy names, and the user of your functions will have to remember the right fancy names.
And all he/She wanted was to have the absolute value of some numerical variable...
One action means one and only one function name.
Note that you are not limited to change the type of one paramter. Anything can change, as long as it makes sense.
2. For operators, it is mandatory
Let's see of operators:
In the above example, you do want to avoid using anything else than the + operator.
Note that C has implicit operator overloading for built-in types (including C99 complex type):
So even in non-object languages, this overloading thing is used.
3. For objects, it is mandatory
Let's see the use of an object basic methods: Its constructors:
Some could consider this like function overloading, but in fact, it is more similar to operator overloading:
Conclusion: Overloading is cool
In C, when you give the name of the function, the parameters are implicitely part of the signature at call. If you have "double fabs(double d)", then while the signature of fabs for the compiler is the undecorated "fabs", it means that you must know it takes only doubles.
In C++, the name of the function does not mean its signature is forced. Its signature at call is its name and its parameters. Thus, if you write abs(-24), the compiler will know what overloading of abs it must call, and you, when writing it, find it more natural: You want the absolute value of -24.
Anyway, anyone who coded somewhat in any language with operators already uses overloading, be it C or Basic numerical operators, Java string concatenation, C# delegates, etc.. Why? because it's more natural.
And the examples shown above are just the tip of the iceberg: When using templates, overloading become very very useful, but this is another story.
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.