Answer the question
In order to leave comments, you need to log in
Answer the question
In order to leave comments, you need to log in
Once you define your classes, you often need different operations on their instances, and operator overloading helps to present this in a human-readable way.
For example, instances of the Vector3 class need to be added (the usual operation of adding vectors in 3-dimensional space), and in the case without operator overloading, this looked like this
And if you use a static function:
But with the overloaded +
operator , you can get the following
And this can be done as with many operators, not only arithmetic or logical
It is needed to simplify development. When you develop your class and you need to sort by some of the fields, for example, you can compare this field directly, or write an overload of operators and compare instances at once, there are also assignment operators that allow you to copy not only a pointer to data, but also the data itself. Everything that is done in these operators can be done by hand, but by making overloading you make it more convenient for yourself first of all and reduce code duplication, which means that by correcting the code in one place you will correct it everywhere
And how will you compare points in three-dimensional space, for example?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question