Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 837
user2672807 Avatar asked Nov 28 '22 04:11

user2672807


2 Answers

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.

like image 141
dyp Avatar answered Nov 29 '22 18:11

dyp


This happes when general object oriented terms are mixed with C++ related terms.

  • subobject: Any object that is stored within another object (array elements, base class objects and data member objects).
  • sub-class: A general object orientation term refering to what in C++ is called the "derived class"
  • super-class: A general object orientation term refering to what in C++ is called the "base class".
  • complete object: Any object that is not stored within another object.

Just a little hint - When talking in C++ land, I try to use the C++ terminology. When talking in Java land, I'm trying to talk with the Java terminology.

like image 43
Johannes Schaub - litb Avatar answered Nov 29 '22 18:11

Johannes Schaub - litb