C ++ operator overload

When overloading operators, it is necessary to overload> = <= and! =?

It seems to be reasonable if C ++ invoked operator = for! = ,! > for the operator <= and! <for operator> =.

In this case, or is it necessary to overload each function?

+5
source share
9 answers

Yes, this is necessary if you want them all to work the way you want them to work.

C ++ does not lead to any specific semantics for most overloaded operators. The only thing that is fixed is the general syntax for the operator (including unary or binary, and such things as priority and associativity). This immediately means that the actual functionality that you implement when overloaded can be completely arbitrary. In the general case, there cannot be any meaningful connection between what the operator ==does and what the operator does !=. An operator ==can write data to a file, and an operator !=can sort an array.

, , , ++ . , , ! == != ! > <=.

+2

Boost , . .

++ , , < > , ( ).

+7

. boost, boost . -, - .

++ ( ), , (), OP, , operator ==, operator !=. <, >, ++ ..

+3

, operator == operator <, :)

(EDIT: . std:: rel_ops;)

+2

, , , . ++ , ; , , , . RTL < =.

+1

, , CRTP, (. ) .

0

, . , . , , .

0

++ , , . , operator+, , operator+= ( Ruby Scala). , < ==, , <= . , ==, , != ( Ruby Scala). , operator * () , operator -> .

std::relops ( ) , Boost , < ==.

0
source

All Articles