Why must the copy assignment operator return a reference/const reference?

Strictly speaking, the result of a copy assignment operator doesn’t need to return a reference, though to mimic the default behavior the C++ compiler uses, it should return a non-const reference to the object that is assigned to (an implicitly generated copy assignment operator will return a non-const reference – C++03: 12.8/10). I’ve seen a … Read more

Operator overloading ==, !=, Equals

As Selman22 said, you are overriding the default object.Equals method, which accepts an object obj and not a safe compile time type. In order for that to happen, make your type implement IEquatable<Box>: public class Box : IEquatable<Box> { double height, length, breadth; public static bool operator ==(Box obj1, Box obj2) { if (ReferenceEquals(obj1, obj2)) … Read more

Operator[][] overload

You can overload operator[] to return an object on which you can use operator[] again to get a result. class ArrayOfArrays { public: ArrayOfArrays() { _arrayofarrays = new int*[10]; for(int i = 0; i < 10; ++i) _arrayofarrays[i] = new int[10]; } class Proxy { public: Proxy(int* _array) : _array(_array) { } int operator[](int index) … Read more

operator

The problem is that you define it inside the class, which a) means the second argument is implicit (this) and b) it will not do what you want it do, namely extend std::ostream. You have to define it as a free function: class A { /* … */ }; std::ostream& operator<<(std::ostream&, const A& a);

__lt__ instead of __cmp__

Yep, it’s easy to implement everything in terms of e.g. __lt__ with a mixin class (or a metaclass, or a class decorator if your taste runs that way). For example: class ComparableMixin: def __eq__(self, other): return not self<other and not other<self def __ne__(self, other): return self<other or other<self def __gt__(self, other): return other<self def __ge__(self, … Read more