Per [intro.object]/2:
[..] An object that is not a subobject of any other object is called a complete object [..].
So consider this snippet of code:
struct Base {};
struct Derived : Base {};
struct MostDerived : Derived {};
I can't understand the wording in this quote from the standard:
[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.
From the quote what I understand is that a type of a complete object is of "most-derived" class type. I stopped here, I really do not understand the rest of the wording.
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. Is this true?
If I have created an object of Base like this: Base b_obj = Base(), is the object b_obj a "most-derived" object?
If I have created an object of Derived like this: Derived d_obj = Derived(), is the object d_obj also a "most-derived" object?
Does 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?
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?.
"Most derived class" is supposed to be dependent on the object under consideration. It is not a property of the class as such.
if I have created an object of Base like this: Base b_obj = Base(), Is the object b_obj is "most-derived" object?
Yes, b_obj is a variable and as such a complete object. A complete object is always a most-derived object.
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?
Same as above applies.
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" means that there is no other object of which the object under consideration is a base class subobject. This is again not a property of classes themselves, but depends on the concrete instance of the class. If for example a Derived object is created with new Derived, then it contains a Base base class subobject and this subobject is not a most-derived object. However the Derived object is the most-derived object, since there isn't e.g. any MostDerived object of which it is a base class subobject.
Is every "complete" object is "most-derived" object
Yes, every complete object is a most-derived object. But the reverse is not true. For example
struct A {
Base b;
};
A a;
a is an object of type A and itself a most-derived object and a complete object, but a.b is also a most-derived object, although not a complete object.
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
No. A most-derived object is an object of a most-derived class, and a most-derived class must be of a class type. Objects may be of class type, but non-class type objects also exist.
So if I have created an object of Base like this:
Base b_obj = Base(), Is the objectb_objis "most-derived" object?
Yes. The most-derived object of b_obj is an object of type Base. This is not necessarily a complete object, however, since this could be a class member definition. Again, complete is not synonymous with most-derived.
Also if I have created an object of Derived like this:
Derived d_obj = Derived(), Is the objectd_objis also a "most-derived" object?
Yes. The most-derived object of d_obj is an object of type Derived.
If you have an object created as type MostDerived:
MostDerived md;
MostDerivedDerivedBaseMostDerivedDerived, which is neither a complete object nor a most-derived objectDerived has a subobject of type Base, which is neither a complete object nor a most-derived object.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With