title: static_cast derived this object to base class in C++
body: <p>When reading Item 27 Minimize casting in <strong>Effective C++</strong>, it says do not try to use <code>static_cast</code> to cast <code>*this</code> in derived class to base class. It because <strong><code>static_cast&lt;Base&gt;(*this)</code> will create a temporary object of Base class</strong>. I tried an example as follows, however, it always output 10 using different compilers such as clang 3.8 and gcc 4.9, 5.3. </p>
  
  <p>Am I wrong? </p>
  
  <pre><code>  #include &lt;iostream&gt;
  
    class A {
    public:
      int a;
      virtual void foo() {std::cout &lt;&lt; a &lt;&lt; std::endl;}
    };
  
    class B : public A {
    public:
      int b;
      void foo ()  { static_cast&lt;A&gt;(*this).foo();}
    };
  
  
    int main () {
      B b;
      b.a = 10;
      b.foo();
  
      return 0;
    }
  </code></pre>
  
  <p>The question is why <code>static_cast</code> will create a temporary object.</p>