Inability to reload Dot '.' operator in c ++

It’s hard for me to understand the explanation from strustrup about the difficulties that need to be encountered if the operator overloads for '.' was allowed.

See this quote from Bjarne Stroustrup:

Operator

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

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? } 

In the example above, why when running xf() ?

there is any confusion
 Y& operator.() { return *p; } 

That's what I think:

  • operator. () is called on x, so it is not obivous and intuitive, what Y& operator.()( return *p; } should be called?
  • After returning *p , which points to an object of type Y and, therefore, Y::f() should be called finally (not X::f() )

What am I missing in explaining strustup? Why is it not easy?

+5
source share
1 answer

Some progress has been made: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4477.pdf . Due to some technical issues, this will not be in C ++ 17, but I hope to see it for C ++ 20.

+9
source

Source: https://habr.com/ru/post/1264116/


All Articles