Are base class objects implicitly added to derived classes?
I have just read about subobjects in the Standard saying this:
A subobject can be a member subobject (9.2), a base class subobject (Clause 10), or an array element.
I have encountered a lot of situations when reading about C++ where subojects were mentioned without even having a subobject(explicitly defined) in the derived class.
But does this mean that whenever a class has a base class, the compiler implicitly adds an object of the base class type in the derived class?
Like:
class Base
{
};
class Derived : Base
{
};
So a subobject of Base has been added in Derived?
Is there something in the Standard that I should have read? I am aware of that it was a very tiny quotation though.
Update:
If we had these classes:
class Base
{
int anint;
float afloat;
};
class Derived : Base
{
//inherited member variables...
};
So in the code above, are anint
, afloat
subobjects of Base
? Are anint
and afloat
also subobjects of Derived
? AND is there also added a member to Derived
that looks like Base something
? So in the end, Derived
has three subobjects: anint
, afloat
and Base something
?
Well, yes. The most derived class will contain sub-objects of each class it derives from. To quote C++17 (n4659 draft), [class.derived/3]:
The base-specifier-list specifies the type of the base class subobjects contained in an object of the derived class type. [ Example:
struct Base {
int a, b, c;
};
struct Derived : Base {
int b;
};
struct Derived2 : Derived {
int c;
};
Here, an object of class Derived2 will have a subobject of class Derived which in turn will have a subobject of class Base. — end example ]
The term sub-object is used more generally because inheritance is not the only way to form aggregate types. You can add members to a class, those will be sub-objects as well. And when you form arrays, each element is a sub-object of the array as a whole.
To address your update to the question:
So in the code above, are anint
, afloat
subobjects of Base
?
Yes. They are members of Base
and therefore sub-objects of it. Their memory location is laid out inside the memory of a Base
object.
Are anint
and afloat
also subobjects of Derived
?
Yes, on account of inclusion being transitive. Derived
contains a sub-object of Base
.
AND is there also added a member to Derived
that looks like Base
something?
There is no named member for the Base
sub-object. There is only a chunk of memory inside a Derived
object that has a Base
object constructed in it.
So in the end, Derived has three subobjects: anint, afloat and Base something?
Yes. It looks like somewhat this:
+----------------------+
|+--------------------+|
|| int | float ||
|+--------------------+|
| Base object (unnamed)|
+----------------------+
Derived object
But does this mean that whenever a class has a base class, the compiler implicitly adds an object of the base class type in the derived class?
Yes. That is what being a derived class is, along with having access to the protected members of that base sub object, and being implicitly convertible to that base sub object.
链接地址: http://www.djcxy.com/p/78948.html上一篇: Java,参数中有3个点
下一篇: 基类对象是否隐式添加到派生类中?