How to overload the operator !=?

Discussion in 'Ruby' started by ankyhe@gmail.com, Dec 13, 2006.

  1. Guest

    I overload ==, >, <, >=, <=successfully, however I can't overload != .
    How to overload operator !=?
     
    , Dec 13, 2006
    #1
    1. Advertising

  2. Vlad Galu Guest

    On 12/13/06, <> wrote:
    > I overload ==, >, <, >=, <=successfully, however I can't overload != .
    > How to overload operator !=?


    Overloading "==" should do it :)

    >
    >
    >



    --
    If it's there, and you can see it, it's real.
    If it's not there, and you can see it, it's virtual.
    If it's there, and you can't see it, it's transparent.
    If it's not there, and you can't see it, you erased it.
     
    Vlad Galu, Dec 13, 2006
    #2
    1. Advertising

  3. Jan Svitok Guest

    On 12/13/06, Vlad Galu <> wrote:
    > On 12/13/06, <> 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 ==
     
    Jan Svitok, Dec 13, 2006
    #3
  4. Vlad Galu Guest

    On 12/13/06, Jan Svitok <> wrote:
    > On 12/13/06, Vlad Galu <> wrote:
    > > On 12/13/06, <> 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 :)

    >



    --
    If it's there, and you can see it, it's real.
    If it's not there, and you can see it, it's virtual.
    If it's there, and you can't see it, it's transparent.
    If it's not there, and you can't see it, you erased it.
     
    Vlad Galu, Dec 13, 2006
    #4
  5. Jan Svitok Guest

    On 12/13/06, Vlad Galu <> wrote:
    > On 12/13/06, Jan Svitok <> wrote:
    > > On 12/13/06, Vlad Galu <> wrote:
    > > > On 12/13/06, <> 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 :)


    Right, I just though some clarification would not harm ;-) I should've
    written that...
     
    Jan Svitok, Dec 13, 2006
    #5
  6. Tom Werner Guest

    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
     
    Tom Werner, Dec 13, 2006
    #6
  7. On 13.12.2006 20:33, Tom Werner 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
     
    Robert Klemme, Dec 13, 2006
    #7
  8. Tom Werner Guest

    Robert Klemme wrote:
    > On 13.12.2006 20:33, Tom Werner 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
     
    Tom Werner, Dec 13, 2006
    #8
  9. On Wednesday 13 December 2006 22:55, Robert Klemme wrote:
    > On 13.12.2006 20:33, Tom Werner 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 ;-))
    --
    Sylvain Joyeux
     
    Sylvain Joyeux, Dec 14, 2006
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mr Smith
    Replies:
    7
    Views:
    468
    Mr Smith
    Feb 10, 2005
  2. Matthew Monopole

    overload operator -

    Matthew Monopole, Aug 8, 2003, in forum: C++
    Replies:
    1
    Views:
    479
    Jakob Bieling
    Aug 8, 2003
  3. Jakob Bieling

    Q: Overload operator new/delete

    Jakob Bieling, Aug 8, 2003, in forum: C++
    Replies:
    0
    Views:
    400
    Jakob Bieling
    Aug 8, 2003
  4. Piotre Ugrumov
    Replies:
    3
    Views:
    375
    Nick Hounsome
    Jan 25, 2004
  5. Ying-Chieh Liao

    function overload (not operator overload)

    Ying-Chieh Liao, Oct 11, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    255
    Sherm Pendley
    Oct 11, 2004
Loading...

Share This Page