1

I got that subobjects are member subobjects, base class subobjects and arrays. I couldn't find anything that explicit explain the two first terms. In the following code for example:

struct A{int a;};
struct B{int b;};
struct C:public A,public B{};

I think that: int a is a member subobject of a possible, not yet instantiated, object of type A; int a is a base class subobject of a possible, not yet instantiated, object of type C. Is it Right? What is the definition of member subobject and base class subobject? Could you provide examples?

  • "I got that subobjects are member subobjects, base class subobjects and arrays." Where did you get those terms from? What does *that source* say about the terms? – Karl Knechtel Oct 27 '21 at 12:37
  • No, `a` is a member of class `A` (it is also a member of class `C` since C **is** an `A`). There is no such thing as _"member subobject"_. – Fareanor Oct 27 '21 at 12:38
  • 2
    "Objects can contain other objects, called subobjects. A subobject can be a member subobject (11.4), a base class subobject (11.7), or an array element." 6.7.2(Object Model) C++ ISO draft 2020 –  Oct 27 '21 at 12:42

2 Answers2

4

Whenever a class inherits from another one it inherits, too, an instance of that class:

class A { };
class B : A { };

Then class B internally looks like:

class B
{
    A a; // <- implicit base class sub-object, not visible to you
};

Note that in some cases there might be even be more than one A!

class A { };
class B : A { };
class C : A { };
class D : C, B { };

D then internally looks like:

class D
{
    B b; // { A a; }
    C c; // { A a; }
};

with b and c being the base class sub-objects; or in a flattened representation:

class D
{
    A aFromB;              // inherited from B, but not a sub-object of D itself
    // other members of B
    A aFromC;              // inherited from C, again not a sub-object of D
    // other members of C 
};

B and C base class sub-objects are not visible in this representation, still they are there in form of the respective A instance combined with the respective other members (think of having braces around).

If you want to avoid duplicates of A, you need to inherit virtually: class B : virtual A { } – all virtually inherited (directly or indirectly) instances of A are then combined into one single instance (though if there are non-virtually inherited ones these remain in parallel to the combined one), consider:

class A { };
class B : A { };
class C : virtual A { };
class D : virtual A { };

class E : A, B, C
{
    A combinedAFromCAndD;
    // other members of B
    // other members of C
    A separateAFromD
    // other members of D
};

Note: These layouts above are just examples, concrete layouts might vary.

Aconcagua
  • 24,880
  • 4
  • 34
  • 59
  • Clarified a lot . But When you write how class D looks like internally, `B b` and `C c` wouldn't be subobjects members of a instance of D? I think i'm missing the member subobject definition now.... –  Oct 27 '21 at 14:09
  • 1
    `B b` and `C c` are – just like in very first example – base class subobjects, and again you don't actually see them – apart from that you can access their (non-private) members as if they were part of the inheriting class, i.e. `this->someFunction()` instead of `this->b.someFuntion()`. – Aconcagua Oct 27 '21 at 14:53
  • Do you know where I can find more about it? I have searched the term over The C++ programming language by Bjarne, Modern C++ design by Alexandrescu and didn't find anything, the Draft is overwhelming me in this topic and it don't provide explicit definitions(atleast I couldn't find them) –  Oct 27 '21 at 15:24
  • 1
    Any good [C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) (maybe skipping the memory layout part, though, for which you might find [this question](https://stackoverflow.com/questions/25137705/c-memory-layout-of-classes-using-inheritance) interesting). Candidates for search terms to get more information might be `inheritance`, `virtual inheritance` and `memory layout` as well as combinations of – all of these combined with `c++`, of course... – Aconcagua Oct 28 '21 at 07:30
2

In your code, the base class subobjects of instances of C are the instances of A and B contained inside it. Each of those subobjects has a subobject itself (a and b); those are not considered subobjects of the instance of C (because they're subobjects of subobjects), but are considered "nested objects" of the instance of C.

Sneftel
  • 40,271
  • 12
  • 71
  • 104
  • I would rather say "(class) members" instead of "nested objects". – Fareanor Oct 27 '21 at 12:39
  • 1
    @Fareanor Sure, but "nested objects" also includes, e.g., members of class members, which are not class members (of the outer class). The term encompasses all instances of "B is located inside A's allocation". – Sneftel Oct 27 '21 at 12:41
  • You're right indeed. But in the OP's example, I think it's useful to make explicit that `a` is not nested at all, it is a member of `C` since inheritance is not composition. – Fareanor Oct 27 '21 at 12:44
  • @Sneftel, I'm still a little lost with a lot of "subobject/object" I've already read. Could you write some code to ilustrate? –  Oct 27 '21 at 12:51
  • @Fareanor Colloquially, yes. Pedantically, not sure. – user253751 Oct 27 '21 at 12:52
  • 2
    @Fareanor The OP is specifically concerned with the section of the standard which covers objects, including objects of class type, but also including arrays. That section formally defines the "nested within" concept such that `a` is "nested within" an instance of `C`. It also defines member subobjects as distinct from base class subobjects, meaning `a` is a "class member" of `C`, but it is *not* a "member subobject" of an instance of `C`. – Sneftel Oct 27 '21 at 12:53
  • 1
    @Roman I'm not sure what code you'd want me to write. People will use the term "subobject" quite broadly, and not in the same specific, limited way the Standard uses it, so if you're trying to match up those definitions it's not going to work. – Sneftel Oct 27 '21 at 13:00
  • @Sneftel So, all member subobjects of a instance L of class C are class members of C, but not all class members of C are member subobjects of the instance L? –  Oct 27 '21 at 14:26
  • 1
    @Roman Correct. But note that the formal term "member subobject" (or subobject in general) isn't widely used, it's mainly used by the standard to define recursive concepts (e.g. an object is POD only if all of its subobjects are POD). – Sneftel Oct 27 '21 at 16:30