Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
513 views
in Technique[技术] by (71.8m points)

inheritance - C++ Disambiguation: subobject and subclass object

Basically what the title says. I have been referring to Base objects as subobjects. Is that correct and would it also be correct that subobject == superclass object? Which one is preferred?

The subclass means the derived class and the subclass object means the derived class' object, right?

The confusion for me is that subclass object != subobject.

If any of this is right, anyway..

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The C++ Standard has a clear definition of what a subobject is. That said, many people don't (precisely) use the language of the Standard when talking about C++. One popular example is the term object. From languages like Java, some people tend to use object only in the sense of instance of a class, which wouldn't apply to int. In the terms of the C++ Standard, an int is an object.

What the Standard says in [intro.object]/2:

Objects can contain other objects, called subobjects. A subobject can be a member subobject, a base class subobject, or an array element. An object that is not a subobject of any other object is called a complete object.

This is to be understand in the context of the (possible) memory layout. It isn't fully specified, but most likely looks like this:

class Foo { int i; };
class Bar : public Foo { int j; };

An object of type Bar could look like this in memory:

+-Bar-------------------+
|  +-Foo-----+          |
|  |  int i  |  int j;  |
|  +---------+          |
+-----------------------+

That is, the members of Foo are members of Bar like the direct members of Bar. Every object of Bar therefore "contains" an object of type Foo. Also consider

Bar b;
Bar* pBar = &b;
Foo* pFoo = &b;
+-Bar-------------------+
|  +-Foo-----+          |
|  |  int i  |  int j;  |
|  +---------+          |
+--^--------------------+
^  |pFoo
|pBar

To allow pFoo to point to complete objects of type Foo and to subobjects of type Foo, there needs to be a whole and (memory-wise) independent object of type Foo inside any object of type Bar. It might not be known at compile-time which one is the case, but the code produced for pFoo->i = 5; has to work for both.

This all is specified under the as-if rule, i.e. it doesn't have to be that way, but it has to observably behave that way. Also, it isn't required that this is the actual memory layout, but it is a common implementation.


In [intro.object]/4

If a complete object, a data member, or an array element is of class type, its type is considered the most derived class, to distinguish it from the class type of any base class subobject; an object of a most derived class type or of a non-class type is called a most derived object.

There's no use of the word super in the Standard other than supersede and superset. There's base class and derived class (as well as ~ object).


Outside the C++ language Standard, the term superclass is used to refer to a base class, and the term subclass is used to refer to a derived class. This refers to the OO concepts and classification, much like a Species is a sub-category of a Genus in biology.

But in this categorization, there's no memory layout, hence no need to talk about subobjects. There are instances or objects (in the OO sense) of classes, so you could talk about objects of a superclass and objects of a subclass. The confusion might stem from abbreviating this object of a subclass to subobject, and mixing language of OO with language from the C++ Standard.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...