Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

That's because operator overloading in C++ isn't commutative in general. For integers, 'a + b' is the same as 'b + a', but it's not the same in C++.

For example, I could do:

    class MyClass {int operator+(int a) { return a; }};

    MyClass x;
    x + 1;  // Valid.
    1 + x;  // Type error.
EDIT: My mistake. C++ requires these operators to be nonstatic member functions:

     = -> [] ()
Which, annoyingly enough, means that you can define '+' over types that you haven't implemented, but you can't define [] or =.


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: