16
votes

As per the comment under this answer, references were introduced primarily to support operator overloading which quotes Bjarne Stroustrup:

References were introduced primarily to support operator overloading. C passes every function argument by value, and where passing an object by value would be inefficient or inappropriate the user can pass a pointer. This strategy doesn’t work where operator overloading is used. In that case, notational convenience is essential so that a user cannot be expected to insert address− of operators if the objects are large.

Which implies that operator overloading can't work with pointer. But it doesn't clearly explain why operator overloading with pointers can't work. Why wouldn't operator overloading work for pointers?

IMO where references are used, pointers can also be used in its place.

9

9 Answers

7
votes

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.

5
votes

Why doesn't it work for pointers? Because it's ambiguous. Would

ostream* operator<<(ostream* s, const char* c);

match

cout << 'a';

or

cout << "a";

?

Also, you can't use address-of (&) with a temporary. What should this do:

complex<double> a, b, c;
cout << a + b * c;

since b * c is a temporary, and the sum is also.

?

1
votes

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.

1
votes

Because most operators already have an alternate established meaning when applied to pointers.

1
votes

Operator overloading work on objects but pointer is not an object by itself. It points to an object.

1
votes

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.

0
votes

Operators can't be overloaded when both types involved are built-in types (like int, float or any kind of pointer). You can overload operator for one class and one primitive type (or you can just use type conversion). For example you can add std::string and const char *.

0
votes

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.

0
votes

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.