I Don't Understand The -> Operator For std::shared_ptr
c++, c++11, methods, operator-overloading, operators
Solution
How can returning a pointer substitute for something like `->->` or `operator->()->` syntax?
Because the standard says so.
13.5.6 [over.ref] defines an overloaded `operator->` to Do The Right Thing:
An expression `x->m` is interpreted as `(x.operator->())->m` for a class object `x` of type `T` if `T::operator->()` exists and if the operator is selected as the best match function by the overload resolution mechanism (13.3).
If it didn't do that, then you couldn't implement types that act like pointers and have the usual semantics for `x->m` that users expect from a pointer-like type. So the language says that's what it does, doing anything else would make overloading `operator->` far less useful.
This actually allows you to chain calls to `operator->` to arbitrary depths, if you have a pointer-like type that returns a pointer-like type that returns a pointer-like type etc. until finally something returns a real pointer.
Problem
I can't seem to wrap my head around it. The `->` operator for `std::shared_ptr`, only returns a pointer to the member (let's say it is a instance of a class), yet at the same time it can be used to call methods... HOW? We are not doing anything to the pointer returned by the `->` operator. How do we call methods, and access instance variables? If I have something functionally equivalent, such as ``` T* Get() { return &t; } ``` in `std::shared_ptr` (and yes, I do realise there is a `get()` method), why can I not write: ``` someSharedPtr.Get()SomeMethod() ``` That is essentially what ``` someSharedPtr->SomeMethod() ``` is doing... I am confused. How can returning a pointer substitute for something like `->->` or `operator->()->` syntax?