Why isn’t operator overloading for pointers allowed to work?

Because if it was allowed, then it would not look good, and wouldn’t be as intuitive as its with reference.

Suppose it is allowed, then you would write:

struct A{};
A a, *pa, b;

a = pa ;//doesn't look good, also not intuitive. (not real C++)

It doesn’t look good, because on left side you’ve non-pointer, on right side you’ve pointer. Looks very very weird. Also, since the types don’t match, it doesn’t look very intuitive as to what exactly its doing. I mean, you’re assigning pointer to a non-pointer; what such an assignment is supposed to do? Copying the content of the address pointed to by pointer to the destination (non-pointer) is not very inttuitive.

On the other hand, as its allowed with reference (the reality, not a supposition):

a = b; //looks good, intuitive, as now both side is same type

With reference, you’ve both side same type, its only when b gets passed to operator=() as argument, it is passed by reference (or say by pointer, as references are syntactic sugar of pointers.) to avoid unnecessary copy, which in turn doesn’t hinder performance, as it would if it is passed by value.

It would be also interesting to note that not only b is passed by reference (or pointer underneath), a also gets passed to the function by pointer, because we know in the function, the keyword this is actually a pointer.

So references were introduced in C++, to make whole thing look good and intuitive for programmers, otherwise they’re pointers underneath. In fact, most compilers implement references using pointers (pointer-mechanism) internally.

Leave a Comment