3 ms·
Moreover, 3[a] appears to be supported solely for backwards compatibility with C since it works only for pointers - you can't do this with classes which define
by qb45 14y ago
Moreover, 3[a] appears to be supported solely for backwards compatibility with C since it works only for pointers - you can't do this with classes which define operator[](int).
- ori_b 14y agoThat'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 =.