user103214
user103214

Reputation: 3658

Confusion with complete objects and sub-objects

class A { 
 int x; 
};

class B : public A {
 int y;
};

B b;

I know b is considered a complete object of B and the object that B contains is a sub-object of A.

But someone pointed me to this,

Paragraph 3, of C++ Standard 1.8

(1.8/3) For every object x, there is some object called the complete object of x, determined as follows: — If x is a complete object, then x is the complete object of x. — Otherwise, the complete object of x is the complete object of the (unique) object that contains x.

In this, I'm finding the wording the very confusing with all those xs all over the place, Can you explain this with my example?

Upvotes: 1

Views: 799

Answers (3)

Kerrek SB
Kerrek SB

Reputation: 477378

The quote is from the C++11 standard, 1.8. More completely:

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

3) For every object x, there is some object called the complete object of x, determined as follows:

  • If x is a complete object, then x is the complete object of x.

  • Otherwise, the complete object of x is the complete object of the (unique) object that contains x.

In other words, consider this:

struct A { }; struct B : A { };

A x[10];
B y;

Now x[1] is an object whose complete object is x, and y::A is an object whose complete object is y (and both subobjects are of type A).

It's just terminology...

Upvotes: 3

Xeo
Xeo

Reputation: 131829

b is the complete object of b, x, y and the A subobject of b.

Upvotes: 1

Jeremy Friesner
Jeremy Friesner

Reputation: 73181

It sound to me that by "complete object" they are referring to the object that was returned by the new operator, or placed onto the stack as a local variable. For example, if we start with your code (above), b is the complete object for x. How did we get that? By working our way up the hierarchy:

  1. Is (x) its own complete object? No, it is a member of A, and thus a part of a larger object.
  2. Is (A) its own complete object? No, it is a superclass of B, and thus a part of a larger object.
  3. Is (B) its own complete object? Yes, because the object b was declared as being of type B.

(If b had instead been part of a still-larger object, then we would have had to go further up the hierarchy to find the complete object)

Upvotes: 3

Related Questions