Why can't you overload '.' statement in C ++?

It would be very helpful to overload. operator in C ++ and return a reference to the object.

You can overload operator-> and operator* , but not operator.

Is there a technical reason for this?

+66
c ++ operator-overloading
Feb 06 '09 at 11:54
source share
4 answers

See this quote from Bjarne Stroustrup :

Operator

. (dot) can in principle be overloaded using the same method used for β†’. However, this may raise questions about whether the operation is intended to overload the facility. or the object referenced. For example:

 class Y { public: void f(); // ... }; class X { // assume that you can overload . Y* p; Y& operator.() { return *p; } void f(); // ... }; void g(X& x) { xf(); // X::f or Y::f or error? } 

This problem can be solved in several ways. At the time of standardization, it was not obvious how it would be better. For more details, see C ++ Design and Evolution .

+51
Feb 06 '09 at 12:07
source share

Stroustrup said C ++ should be an extensible but not mutable language.

The point operator (access to attributes) was considered too close to the core of the language to allow overloading.

See Project and evolution of C ++ , p. 242, section 11.5.2. Intelligent links.

When I decided to allow operator overloading -> , I naturally thought that the operator . may be similarly overloaded.

At that time, I considered the following arguments convincing: if obj is an object of a class, then obj.m matters for every member m this class of the object. We try not to change the language by overriding the built-in operations (although this rule is violated for = because of extreme necessity and for unary & ).

If we allowed an overload . for class X , we would not be able to access elements of X usual ways; we would have to use the pointer and -> , but -> and & could also be overridden. I need an extensible language, not mutable.

These arguments are valid, but not conclusive. In particular, in 1990, Jim Adcock suggested allowing operator overloading . just like the operator -> .

The self in this quote is Bjarne Straustrup. You cannot be more authoritative than that.

If you really want to understand C ++ (as in β€œwhy is this”), you should absolutely read this book.

+46
Feb 06 '09 at 12:09
source share

Stroustrup has the answer to this question :

Operator

. (dot) can in principle be overloaded using the same used for β†’. However, this may lead to an operation intended for an object overload. or an object referred to from. For example:

 class Y { public: void f(); // ... }; class X { // assume that you can overload . Y* p; Y& operator.() { return *p; } void f(); // ... }; void g(X& x) { xf(); // X::f or Y::f or error? } 

This problem can be solved in several ways. During standardization, it was not obvious how the best. For more information, see D&E .

+27
Feb 06 '09 at 12:07
source share

It is very easy to understand if you go through the internal mechanism of calling the operator function. Let's say that a class of classes can have two members r for the real part and I for the imaginary part. Say Complex C1 (10,20), C2 (10,2) // we assume that the class already has a constructor with two arguments. Now, if you write C1 + C2 as an instruction, then the compiler will try to find the overloaded version of the + operator on the complex number. Now suppose that I overload the + operator, so C1 + C2 internally translates as c1.operator + (c2) Now suppose that creatures you can overload with the "operator. So now think about the next call to C1.disp () / / display the contents of a complex object. Now try to imagine C1.operator as an internal representation . (------) , completely messy things are created. That's why we cannot overload. " operator

+1
Aug 26 '15 at 9:25
source share



All Articles