2

I'm using the document number 4901, C++ Draft ISO 2021, specifically 6.5.2 (Member Name Lookup). I'm failing in understanding a lot of uses of the terms "member subobject" and "base class subobjects". I already asked about these terms in : What is a member sub object? and What is a base class subobject

The Second question had an answer relatively satisfactory for me, the first one although didn't help me. I'm thinking that the explanation in the draft is a little too abstract, so I would rely on a rigorous defitnion of the terms cited above, but really didn't find any. Taking another path, How the member name look up occurs in practice? How the terms: member subobject and base class subobject are related to member names lookup?

  • 2
    The exact mechanism is a compiler-internal implementation detail. The standard only specifies what the result should be, and it's not clear what you believe is missing from the standard text in that regard. – molbdnilo Oct 28 '21 at 15:00
  • In the merge process cited in 6.5.2 there are a lot of use of these two types of subobjects, which are not well defined in the draft (in my opinion) –  Oct 28 '21 at 15:03
  • A "member subobject" is a subobject that is a member. A "base class subobject" is a subobject that is an instance of a base class. (This is normal English.) I don't see how anyone needs more definition than that. – molbdnilo Oct 28 '21 at 15:13
  • How can I know what is normal english, what is language specification? In 6.7.2 it's said that a object can be these two and a array. These two terms are in itallic. In 6.2 Declarations are said to (re)introduce names and entities, but no change in the style of the word occurs and is not just a english word here(I think) –  Oct 28 '21 at 15:18

1 Answers1

4

From an ABI standpoint, there is very little distinction between B and C in the following:

struct A {
  int x;
};

struct B : A {};

struct C {
  A base;
};

Creating an object of type B or C both require creating an object of type A. In both cases, the instance of A belongs to the parent object. So in both cases they are sub-objects.

For objects of type B, the A object is a base class sub-object.

For objects of type C, the A object is a member sub-object.

Edit: integrating stuff from followup questions in the comments.

struct D : A {
  A base;
};

In D's case, there are 2 sub-objects of type A in each instance of D. One base class sub-object and one member sub-object.

  • Clarified much more! In the last two sentences `For B, the A object is a *base class subobject*`. It is for the B class itself or for the instance of B? –  Oct 28 '21 at 15:11
  • 1
    @Roman it's a subobject of instances. –  Oct 28 '21 at 15:12
  • Frank, what if there was a `A child` inside B? Something Like: `struct B : A {A child;};`. Would this be a base class subobject or member subobject? –  Oct 28 '21 at 15:46
  • 1
    @Roman There would be 2 subobjects, one base class subobject AND one member subobject. –  Oct 28 '21 at 15:47
  • But child is a subobject and a instance of a base class right? What I understood from the molbdnilo comment is that it should be a base class subobject.So Just the implicit object created are base class subobject ? –  Oct 28 '21 at 16:03
  • 1
    @Roman no, The A subobject and the A base class are two separate objects. Think of it like a struct having 2 members with different names and the same type. `struct Vec2 {int x; int y;}` –  Oct 28 '21 at 16:22
  • 1
    Good explanation. To add a bit more from programmer's (as opposed to ABI) perspective: *base class sub-object* is *inherited* and unnamed whereas *member sub-object* is *embedded* and can be accessed by its name. Inheritance is related to the "A-**is**-B" relation, whereas embedding with the "A-**has**-B" relation. All this relates to HOW the subobject can be used in a program in a valid way. – zkoza Oct 28 '21 at 17:50