base pointer to derived class

Posted by Jay on Stack Overflow See other posts from Stack Overflow or by Jay
Published on 2013-10-26T03:42:42Z Indexed on 2013/10/26 3:54 UTC
Read the original article Hit count: 127

Filed under:
|
|
|

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?

© Stack Overflow or respective owner

Related posts about c++

Related posts about pointers