Why do we need operator overloading?
You can write code for greater digestibility.
- 3The most harmful lotion OOP. To the uninitiated completely confused. - avp
- 2avp, do you program in java? sometimes write MyFirstObject + = MySecondObject; more convenient than MyFirstObject = MyFirstObject.Add (MySecondObject); even though it is almost the same, even perhaps one through the other - Specter
- one(Offtopic) By the way, ad-hoc polymorphism (which, in particular, is used in case of operator overloading) is not a “gadget” of the OOP and perfectly exists without regard to this paradigm. OOP is mainly to polymorphism through subtypes (which, as a rule, is called simply “polymorphism”). - drdaeman
- Now there are still no OOP languages in which operations can be overloaded ??? I do not remember, in hell is it? By the way, actually, I meant overloaded user operations, which does not apply to java. - avp
- 21. ADA is the first internationally standardized PLO language. 2. See haskell overload. - alexlz
3 answers
So that you can perform the same operations with classes as with standard data types. For example:
class CMoneys { int iDol; int iCen; public: CMoneys(int Dollars, int Cents) { iDol=Dollars; iCen=Cents; } CMoneys(const CMoneys& Money); //////Some code////// CMoneys operator +(CMoneys mon) { return (iDol + Money.iDol, iCen + Money.iCen); } }
////////////// In Functions main ///////////
CMoneys M1(1, 2), M2(2, 3), M; M = M1 + M2; /////////////// Понимаем, как М (M1.iDol + M2.iDol, M1.iCen + M2.iCen)
That is, due to operator overloading, we can perform the same operations with our classes as with the built-in data types. Overload is well written here.
- 6Stop Cclass madness! - gecube
- Why "madness"? You do not like this programming style? I always write 'C' before class name, such as letter I 'before int variable or' ch 'before char variable ... - 3JIoi_Hy6
- one
- Not convincing. If you are a good programmer, then you need to know when a class whose name begins with a 'C' will conflict with the one you developed. Moreover, in the libraries of C ++ I have never met classes that conflict with my own. And no one else from my environment met them. So this is a private matter. But if the compiler swears - then yes, then the prefix will have to be changed ... - 3JIoi_Hy6
- oneClasses in C ??? It seems that I missed something when I learned this language - aknew Sept
In some cases, the overload is still more convenient than its functions with names, especially if such operators are introduced outside of programming - for example, actions with polynomials (I hope no one will argue that two polynomials can be added, subtracted and multiplied and get a new polynomial), the addition of sound intensities , actions with complex numbers, actions listed above with sums of money or any other values recorded as X rubles Y kopecks / X feet Y inches, etc.
- It would be desirable to see an example of code for complex numbers ... Stalker is clearer) - Alerr
- 2give the glass fool ... ahem, so he and ... ahem will break up and cut his hands. And if it is correct and wise to use, then no problems should arise. - Specter
- oneBut I think that real functions should precede operators. Those. if addition is necessary, implement the add () function, and then start thinking about operator overloading. At the same time, the overloaded + operator can be easily implemented through the add () function already written and, I hope, debugged. - gecube
- one@avp So I wrote in some cases, overload is not worth much to get involved in and shove it everywhere, which, however, is true for any action. In such creations they did not ask, but you can write everything through the stump-deck so that later you can not find a small mistake in the simplest procedural program without any OOP. - aknew
- 3@Alerr for example, here: dmtsoft.ru/bn/370/as/oneaticleshablon - aknew
Why do you need? And then what is convenient. In fact, to be honest, operator overload is only confusing.
It is clear from where it grows - from a generalization of the principle of overloading functions, because overloaded operators are the same user functions. Well, programmers are creepy lazy and do not like to write stopitsot letters in the program code.
Regarding when to overload and why. This is a much more interesting question. I can say the following: it is advantageous to overload the operators = (practically necessary if the user class contains pointers somewhere and you need to correctly handle the situation of creating a copy of an existing or temporary object of the class), the operator -> (if you implement the class with the semantics of the classical pointer), the operator [] (if you implement an array or collection class and there is a need to access the item of this collection by index), operator () (if you implement the concept of a functor). The rest overload should be treated with caution. First, the essence of the operation should be clear. For example, the + operator must add objects, and not deduct or, say, count taxes. Those. it should be implemented for custom string types or numeric types. But for objects of business logic or modeling physical processes, the use of this operator is irrational - it becomes unclear what the author of the code wanted to say. Therefore, calls to functions with talking names are our method, correct and reliable. Secondly, there is no possibility to redefine the priorities of operators. Third, you should not produce a bunch of overloads of the same operator for the case of different combinations of types. This, coupled with the cast operators, quickly leads to a mess. And in some cases it may surprise the compiler, well, the programmer too.
- oneGreat answer. Conclusions for practice: 1) Do not overload the operators. 2) Avoid someone else's code with overloaded operators (better rewrite it). 3) Do not assume that functions with speaking names allow you to make truly understandable programs. This is an illusion of comments. A good comment is not about what we are doing (this is clear from the code), but about why we are doing it . Otherwise, you will have mistakes that are badly caught with many years of program maintenance. - avp
- Overloading can be used with mathematical operators or at least well-known types such as lists, maps, graphs, etc. But otherwise it does the code unreadable - user184868