每个 "complete" 对象都是 "most-derived" 对象吗?

Is every "complete" object a "most-derived" object?

根据 [intro.object]/2:

[..] An object that is not a subobject of any other object is called a complete object [..].

考虑一下这段代码:

struct Base {};
struct Derived : Base {};
struct MostDerived : Derived {};

我无法理解标准引用中的措辞:

[object.intro]/6:

If a complete object, a member subobject, or an array element is of class type, its type is considered the most derived class [..] An object of a most derived class type or of a non-class type is called a most derived object.

根据我的理解,完整对象的类型是“最派生的”class 类型。到此为止,后面的写法我真的看不懂了。

  1. 对象不是 class。
  2. 对象是 class、数组或 built-in-type 的实例化。
  3. 子对象是 class 成员对象、数组元素或对象的基础 class。
  4. 派生对象(和 most-derived 对象)仅在 class 继承的上下文中才有意义。
void foo() {
    int i = 0; // complete object, but not most-derived (not class type)
}

class A {
   int i = 0; // non complete object, not most-derived
}

void bar() {
    A a; // complete object, but not derived, so can't be "most derived"
}

class B : A { }

void biz() {
   B b; // complete object, derived object, and most-derived object
}

Is every "complete" object is "most-derived" object

没有。 most-derived对象是most-derivedclass对象,most-derivedclass必须是class类型。对象可能是 class 类型,但也存在 non-class 类型的对象。

  • class-type 的每个完整对象都是 most-derived 对象,前提是 class 继承。
  • 一个most-derived对象可能是一个子对象,所以你不能从most-derived推断对象的完整性(但是,你可以推断most-derived对象是class类型)。

So if I have created an object of Base like this: Base b_obj = Base(), Is the object b_obj is "most-derived" object?

是的。 b_obj 的 most-derived 对象是 Base 类型的对象。但是,这不一定是一个完整的对象,因为这可能是一个 class 成员定义。同样,完整不是 most-derived.

的同义词

Also if I have created an object of Derived like this: Derived d_obj = Derived(), Is the object d_obj is also a "most-derived" object?

是的。 d_obj 的 most-derived 对象是 Derived.

类型的对象

如果您创建的对象类型为 MostDerived:

MostDerived md;
  • 它是一个 MostDerived
  • 类型的对象
  • 它是一个 Derived
  • 类型的对象
  • 它是一个 Base
  • 类型的对象
  • 如果不是成员子对象,则为完整对象
  • 其most-derived对象的类型为MostDerived
  • 它有一个 Derived 类型的子对象,既不是完整对象也不是 most-derived 对象
  • Derived类型的子对象有一个Base类型的子对象,既不是完整对象也不是most-derived对象。

Per the question "What does the "most derived object" mean?" I think that (correct me if I am wrong), objects of type "most-derived" class only, like MostDerived, are called "most-derived" objects. Does this true?.

"最派生 class" 应该依赖于所考虑的对象。它不是 class 本身的 属性。

if I have created an object of Base like this: Base b_obj = Base(), Is the object b_obj is "most-derived" object?

是的,b_obj 是一个变量,因此是一个完整的对象。一个完整的对象总是一个 most-derived 对象。

if I have created an object of Derived like this: Derived d_obj = Derived(), Is the object d_obj is also a "most-derived" object?

同上。

Is the word "derived" in "most-derived" mean that the object is an object of a class like MostDerived, or mean that the object has no class subobject in it?

"most-derived" 表示没有其他对象是其正在考虑的对象的基础 class 子对象。这又不是 classes 本身的 属性,而是取决于 class 的具体实例。例如,如果一个 Derived 对象是用 new Derived 创建的,那么它包含一个 Base 基础 class 子对象,而这个子对象不是 most-derived 对象。但是 Derived 对象是 most-derived 对象,因为没有例如任何 MostDerived 对象,它是基础 class 子对象。

Is every "complete" object is "most-derived" object

是的,每个完整的对象都是一个 most-derived 对象。但反之则不然。例如

struct A {
    Base b;
};

A a;

a是一个A类型的对象,它本身也是一个most-derived对象和一个完整的对象,但是a.b也是一个most-derived对象,虽然不是一个完整的对象。