Base class instance in inheritance

1.4k Views Asked by At

Hope I'm not asking something that has been already answered in here.

If my class B inherits from class A, does new B() create two instances in heap where B instance contains pointer to A instance, or there will be created only one instance of B including A members?

4

There are 4 best solutions below

4
Martin Verjans On BEST ANSWER

From Microsoft Inheritance article :

This is how an object is represented in memory, given the Class Publication directly inherits the class Object.

enter image description here

So a inherited class is an object that contains all information about itself but also about its base class.

3
Ferdinand Swaters On

It will create one instance of B, and that B is also an A.

0
evals On

it will create B instance that (because of inheritance) already include A members, if i understood your question well

0
Nick Gallimore On

It creates one instance and you can access all the members of both A and B on that instance. As stated A is of type B as well. I imagine that in the low level code there probably exists a pointer to A.