17

I have hard time understanding the explanation from Stroustrup for what difficulties one must have faced, if operator overloading for '.' was allowed.

See this quote from Bjarne Stroustrup:

Operator . (dot) could in principle be overloaded using the same technique as used for ->. However, doing so can lead to questions about whether an operation is meant for the object overloading . or an object referred to by . 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)
{
    x.f();    // X::f or Y::f or error?
}

In the above example why should there be any confusion while executing x.f() ?

Y& operator.() { return *p; }

Here is what i think:

  1. operator.() is called on x hence isn't it obvious and intuitive that Y& operator.()( return *p; } should be called ?
  2. Upon returning *p which points to object of type Y and hence Y::f() should be called finally ( not X::f() )

What am i missing in Stroustup's explanation? Why is it not straightforward?

Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93
anurag86
  • 1,635
  • 1
  • 16
  • 31
  • "hence `Y::f()` should be called finally" But isn't that confusing to someone reading that code? Normally `x.f()` would call the `f` member of `x`, but in this case that doesn't happen. – Greg Kikola Feb 12 '17 at 02:41
  • @ScottHunter Well, if the example is from Stroustrup and supposed to illustrate confusion, should not it be confusing :)? – AlexD Feb 12 '17 at 02:42
  • This [SO question](http://stackoverflow.com/questions/520035/why-cant-you-overload-the-operator-in-c) talks about the same problem. –  Feb 12 '17 at 02:45
  • @AlexD : Exactly my point. Overloading is meant to be confusing. But if stroustup is saying its not straightfoward then it has to be something. Hence this question – anurag86 Feb 12 '17 at 02:45
  • @AlexD: No you can't. If `x` has a function named `f()`, you wouldn't be able to call that function with the syntax `x->f()`. – Benjamin Lindley Feb 12 '17 at 02:45
  • @Raw N i am referring to the same question and the example from the accpeted answer, which is unclear to me – anurag86 Feb 12 '17 at 02:47
  • @AlexD with the `.` operator overloaded though you would need to provide some way of actually calling `X::f`. That isn't an issue with `->`. – Greg Kikola Feb 12 '17 at 02:48
  • @anurag86: I don't think it is MEANT to be confusing; its not like confusion is a goal. – Scott Hunter Feb 12 '17 at 02:48
  • 1
    @GregKikola In case of overloaded `.`, you could call `f` true non-overloaded `->`. – AlexD Feb 12 '17 at 02:49
  • @AlexD Then every access would need to be through a pointer. I mean, it'd work, but seems kind of strange. – Greg Kikola Feb 12 '17 at 02:51
  • 1
    @AlexD: Yes, `(&x)->f()` works. It's not clear to me what your point is though. How could that be confusing? Where is there any room for ambiguity? `operator.` can be ambiguous because `x` can have a member named `f`, and whatever is returned from `operator.` can also have a member named `f`. – Benjamin Lindley Feb 12 '17 at 03:06
  • Can someone please post a conclusive answer so that i can accept it – anurag86 Feb 12 '17 at 03:50
  • @BenjaminLindley Yes, you are right. Removing some possibly confusing comments... – AlexD Feb 12 '17 at 04:06

1 Answers1

47

There has been some progress: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4477.pdf . Due to some technical problems this won't be in C++17, but I hope to see it for C++20.