Understanding C++ Virtual Methods of Instances Allocated on the Stack
<p>For the following code:</p>

<pre><code>#include&lt;iostream&gt;
using namespace std;

class A{
public:
virtual int f(){return 1;}
};
class B : public A{
public:
virtual int f(){return 2;}
};
int main(int argc,char*argv[]){
    A b=B();
    cout&lt;&lt;b.f()&lt;&lt;endl;
}
</code></pre>

<p>I expect the number <code>2</code> to be printed.  Instead the program prints the number <code>1</code>.</p>

<p>Could someone explain why this is?</p>
