base pointer to derived class
- by Jay
Suppose there are Base class and Derived class.
Base *A = new Base;
Here A is a pointer point to Base class, and new constructs one that A points to.
I also saw
Base *B = new Derived;
How to explain this?
B is a pointer to Base Class, and a Derived class constructed and pointed by B?
If there is a function derived from Base class, say, Virtual void f(), and it's been overridden in Derived class, then
B->f()
will invoke which version of the function? version in Base class, or version that overridden in Derived Class.
What if there is a new function void g()in Derived, is B->g() going to invoke this function properly?
One more is, is
int *a = new double;
or
int *a = new int;
legal?