I overload ==, >, <, >=, <=successfully, however I can’t overload != .
How to overload operator !=?
On 12/13/06, [email protected] [email protected] wrote:
I overload ==, >, <, >=, <=successfully, however I can’t overload != .
How to overload operator !=?
Overloading “==” should do it
On 12/13/06, Jan S. [email protected] wrote:
On 12/13/06, Vlad G. [email protected] wrote:
On 12/13/06, [email protected] [email protected] wrote:
I overload ==, >, <, >=, <=successfully, however I can’t overload != .
How to overload operator !=?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 ==
Which is precisely what I meant
On 12/13/06, Vlad G. [email protected] wrote:
On 12/13/06, [email protected] [email protected] wrote:
I overload ==, >, <, >=, <=successfully, however I can’t overload != .
How to overload operator !=?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 ==
On 12/13/06, Vlad G. [email protected] wrote:
Which is precisely what I meant
Right, I just though some clarification would not harm I should’ve
written that…
[email protected] wrote:
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
Robert K. wrote:
On 13.12.2006 20:33, Tom W. wrote:
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.
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
On 13.12.2006 20:33, Tom W. wrote:
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
On Wednesday 13 December 2006 22:55, Robert K. wrote:
On 13.12.2006 20:33, Tom W. wrote:
And upon reflection, this makes sense, as in any sane world !(f1 ==
f2) will equal (f1 != f2), and so redefining != would be redundant.
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 ;-))