The C++ standard says "... the interpretation of the call of a virtual function depends on the type of the object for which it is called (the dynamic type)" (p. 252) and then
"if a pointer p
whose static type is 'pointer to B
' is pointing to an object of class D
, derived from B
, the dynamic type of *p
is D
" (p. 2). Here B
is a base class and D
a derived class.
This seems to suggest (to me) that if I say
D d;
B *p = new B();
*p = d;
then, if f()
is virtual in B
, p->f()
should call D::f()
, which is wrong. I guess I'm not clear about the meaning of "pointing to an object of class ...". I know, if I say p = &d
, then D::f()
is called, but I would like to know why the above is wrong.