As per the comment under this answer, references were introduced primarily to support operator overloading which quotes Bjarne Stroustrup:
References
Because most operators already have an alternate established meaning when applied to pointers.
With the plus operator overloaded for a heavy class, you would have to write either a + b
(by-value, inefficient) or &a + &b
(ugly and complicated) to add two objects of this class. But with references you get by-ref even when writing a + b
.
Operator overloading work on objects but pointer is not an object by itself. It points to an object.
It's 2019 and you still do not have access to the classes of native types. There is no way to override operators on them.
Pointers are (like char, int, float, etc...) a native type. You can create an object that behaves like a pointer and overload operators [->, *], but that will not overload [->, *] on the the native pointer. You may also, for example, create an object that behaves like an int and overload [+, -, *, /], but that will not overload [+, -, *, /] on the native int.
For pragmatic rational, imagine the chaos if
this->member
had user-defined meaning.
References are an unrelated topic, IMHO.
Which implies that operator overloading cannot work with pointer.
It doesn't imply that at all. It states that providing references is an essential notational convenience so that the programmer doesn't have to liberally use the address-of operator.
It is also the case that operator overloading doesn't work with pointers, but that's basically because there is no syntax for it. Why? Ask Stroustrup.
Your IMO is correct provided a dereference operator is used before the pointer.
Consider the statement
c= a+b
where the + operator has been overloaded. This statement can be interpreted by the compiler as
c=operator+ (a, b)
Since, here addresses of variables are not getting passed, so we cannot collect them in pointer variables.
If we want to collect them in pointer variables , then we have to modify the statement as follows:
c=&a+&b;
But this means that we are trying to add addresses of a and b and not their values. At such times only references can be used to save memory.