How to overload the operator !=?

A

ankyhe

I overload ==, >, <, >=, <=successfully, however I can't overload != .
How to overload operator !=?
 
J

Jan Svitok

Overloading "==" should do it :)

!= is a defined using == i.e. a != b is parsed as !(a == b)
that means: 1. you cannot override !=, 2. you have to override ==
 
V

Vlad Galu

!= is a defined using == i.e. a != b is parsed as !(a == b)
that means: 1. you cannot override !=, 2. you have to override ==

Which is precisely what I meant :)
 
T

Tom Werner

I overload ==, >, <, >=, <=successfully, however I can't overload != .
How to overload operator !=?

The != token is not really a method at all and thus cannot be redefined.
It is, rather, syntactic sugar that calls == and negates the result. You
can see for yourself that this is true:

class Foo
def ==(other)
puts '== called'
true
end
end

f1 = Foo.new
f2 = Foo.new

f1 == f2
f1 != f2

Which outputs

== called
== called

And upon reflection, this makes sense, as in any sane world !(f1 == f2)
will equal (f1 != f2), and so redefining != would be redundant.

Tom
 
R

Robert Klemme

And upon reflection, this makes sense, as in any sane world !(f1 == f2)
will equal (f1 != f2), and so redefining != would be redundant.

It's been a long day and I'm not sure whether my logic fails me here,
but - from the above seems to follow that C++ is potentially insane.
Not that I didn't know that before - but it's a nice outcome of a thread
about operators. :)

robert
 
T

Tom Werner

Robert said:
It's been a long day and I'm not sure whether my logic fails me here,
but - from the above seems to follow that C++ is potentially insane.
Not that I didn't know that before - but it's a nice outcome of a
thread about operators. :)

Half-way related amusing anecdote (from
http://rollerweblogger.org/roller/entry/arguing_with_scott_meyers):

"Speaking of arguing with Scott Meyers... I worked at one of those phone
company research labs back in the 90's and we had enough money to bring
in Scott Meyers to teach us all about C++. Scott was explaining how you
can overload operators and you can even overload the equals sign, when
one student raised his hand. The student explained that there were some
situations in telecommunications systems where A was equal to B, but B
was not equal to A. Scott immediately objected, of course, but the
student went off into some jargon-filled explanation of his particular
problem domain. Scott let the student finish and then said, "if you were
to overload the equals operator so that A was equal to B, but B was not
equal to A, then /I would want to kill you/." That was the end of the
argument."

Tom
 
S

Sylvain Joyeux

Well, I remember a thread on ruby-lang where somebody were trying to build
CSPs (constraint systems) using overloaded operators. In CSPs, you encode
== and != differently and since you can't overload both in Ruby, he could
not do what he wanted.

He could have in C++ (and somebody at my lab did ;-))
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top