Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 420
cpuer Avatar asked May 30 '11 01:05

cpuer


People also ask

Can pointer operator be overloaded?

The class member access operator (->) can be overloaded but it is bit trickier. It is defined to give a class type a "pointer-like" behavior. The operator -> must be a member function. If used, its return type must be a pointer or an object of a class to which you can apply.

Can you use operators on pointers?

operators can be performed even when the pointers point to elements of different arrays. You can assign to a pointer the address of a data object, the value of another compatible pointer or the NULL pointer.

Which operator Cannot be overloaded in C language?

Answer: Scope resolution operator (::) cannot be overloaded in C language.


2 Answers

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.

like image 94
Nawaz Avatar answered Oct 26 '22 23:10

Nawaz


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.

?

like image 34
Ben Voigt Avatar answered Oct 27 '22 01:10

Ben Voigt