I am wondering (just out of curiosity) why operator overloading is not allowed in C ++ for pointers. I mean something like this:
Vector2d* operator+(Vector2d* a, Vector2d* b) { return new Vector2d(ax + bx, ay + by); } Vector2d* a = new Vector2d(1, 1); Vector2d* b = new Vector2d(2, 2); Vector2d* c = a + b;
Note that 'a + b' creates a new Vector object, but then only copies its address to 'c', without calling the copy constructor. Thus, this solves the same problem as the new rvalue links. Also, as far as I know, this is pretty much equivalent to what happens when using operator overloading in C # (but maybe I'm wrong here, I never used C #), and why rvalue refs are not needed in C #.
True, the rvalue reference solution is even better, since it allows you to create objects based on the stack, while this overload will make all Vector2d objects live on the heap, but still it looks like it would be easy to implement in compilers, perhaps for several years before rvalue refs. And with custom allocators, this would not even be so slow.
So is it just illegal because of the principle of "least surprise", or are there other reasons?
source share