C++ virtual function in type casting of reference
<p>I have a question in virtual function and reference. It's from a puzzle when I try to prepare interview. I google it for a while but cannot see the exact case. </p>

<p>The code is given as:</p>

<pre><code>class A{
public:
 virtual void foo() {cout &lt;&lt; "A::foo" &lt;&lt; endl;}
};

class B: public A{
public:
 void foo() {cout &lt;&lt; "B::foo" &lt;&lt; endl;}
};

class C: public B{
public:
 void foo() {cout &lt;&lt; "C::foo" &lt;&lt; endl;}
};

int main(void){
 C c;

 B *q;
 q = &amp;c; q-&gt;foo();
 return 0;
}
</code></pre>

<p>My thought to the output will be B::foo, but the answer is actually C::foo. Can someone tell me why the vtable wont choose B's implementation? Thanks</p>
