简体   繁体   中英

Base class instance in inheritance

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?

它将创建一个B实例,而B也是A.

From Microsoft Inheritance article :

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

在此输入图像描述

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

如果我理解你的问题,它将创建B实例(因为继承)已经包含A成员

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM