title: Is operator-> "chained" for pointers?
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/4896238/overloading-operator">Overloading operator -&gt;</a>  </p>
  </blockquote>
  
  
  
  <p>Hi,</p>
  
  <p>I've seen that <code>operator->()</code> is chained (re-applied) after it is evaluated, for example:</p>
  
  <pre><code>struct Bar
  {
    Bar() : m_str("Hello world!") {}
    const string* operator-&gt;() const { return &amp;m_str; }
    string m_str;
  };
  
  struct Foo
  {
    const Bar&amp; operator-&gt;() const { return m_bar; }
    Bar m_bar;
  };
  
  int main()
  {
    Foo f;
    cout &lt;&lt; f-&gt;c_str() &lt;&lt; endl;
    return 0;
  }
  </code></pre>
  
  <p>works pretty fine, which requires three <code>operator->()</code> to be evaluated - <code>Foo::operator->()</code>, <code>Bar::operator->()</code> and regular pointer resolution.</p>
  
  <p>But it wont work with pointers in the middle - if <code>Foo::operator->()</code> returns pointer to Bar instead of reference, it wont compile. Same happens with <code>auto_ptr&lt;auto_ptr&lt;string>> </code> for example.</p>
  
  <p>Is it specific to non-overloaded <code>operator->()</code> so it is only applied once and does not cause chaining?
  Is it possible to make code below works without using <code>(*ptr2)-> ...</code>?</p>
  
  <pre><code>int main()
  {
    string s = "Hello world";
    auto_ptr&lt;string&gt; ptr1(&amp;s);
    auto_ptr&lt;auto_ptr&lt;string&gt; &gt; ptr2(&amp;ptr1);
    cout &lt;&lt; ptr1-&gt;c_str() &lt;&lt; endl; // fine
    cout &lt;&lt; ptr2-&gt;c_str() &lt;&lt; endl; // breaks compilation
  }
  </code></pre>
  
  <p>Thanks!</p>