Tell me how to rewrite the operators == and! = ???
|
1 answer
Yes, just like the others.
bool operator ==(Type const & left, Type const & right) { ... return ...; } bool operator !=(Type const & left, Type const & right) { ... return ...; }
- oneI did not know what to indicate as friendly, and then determine it outside the class. - doomsday
- @doomsday: In principle, as a member function with one argument. But the external conceptually correct. - VladD
- @VladD, argue, please. Just to expand my horizons. Thank. - BuilderC
- one@BuilderC: the member-function is not symmetrical: in it the first argument (
this
) is highlighted compared to the second. And operations are usually symmetrical. That is, it turns out not very beautiful. This time. Then, if you want to define an operator, in which is the first argument of another type, which is not under your control? For example, you want your string type to add to numbers:5 + MyString("abc")
. Then the member function will not work (since you cannot add a function to anint
), and the external function will roll. That is, the external function always works, and member - sometimes, only if you do not want something strange. - VladD
|