Really sorry about the title, if there is any appropriate one please do change it to that.
I have written below sample code to understand virtual functions.
class A {
public:
virtual int f() {cout << "1";};
};
class B : public A {
public:
int f() {cout << "2";}
};
int main()
{
A a = *new B();
A* c = &a;
c->f();
A* pa = new B();
pa->f();
B* b;
}
The above code outputs 12, I expected that it would print 22.
I thought when a virtual function is invoked from a pointer, it gets dynamically binded. So, I don't understand why the above output is different.