Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

4
  • What does your example illustrate? Are you returning a smart pointer to a string and somehow outputting the size? I'm confused. Commented Sep 21, 2012 at 19:48
  • 2
    It illustrates the last paragraph of my answer, how using the -> operator chains until it gets a raw pointer to something, dereferencing and accessing a member of it. If operator -> didn't chain, the example would be ill-formed as a shared_ptr is not a raw pointer. Commented Sep 22, 2012 at 18:55
  • @LarsViklund: your answer has a problem: you said "operator-> ... automatically chains operator-> calls until one in the chain returns a raw pointer". This is not correct - using A->B syntax chains at most 1 additional call. What the C++ -> binary syntax actually does is not call the object's opeartor-> directly - instead it looks at the type of A and check if its a raw pointer. If it is then -> derefs it and execute B on that, otherwise it calls the object's operator->, derefs the result (either using native raw pointer or another operator-> and then executes B on the result Commented Apr 14, 2013 at 11:03
  • @Guss: I cannot find any chapter and verse for your claim, nor reproduce it in a compiler. C++11 13.5.6/1 indicates that if a suitable overload exists, x->m shall be interpreted as (x.operator->())->m. If the LHS is something that has a suitable overload of operator-> again, this process recurs until there's just the usual (*x).m effect of 5.2.5/2. Commented Mar 20, 2014 at 15:05