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:
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 objectb_obj
a "most-derived" object?If I have created an object of
Derived
like this:Derived d_obj = Derived()
, is the objectd_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?
CodePudding user response:
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.
CodePudding user response:
- An object is not a class.
- An object is an instantiation of a class, an array, or built-in-type.
- Subobjects are class member objects, array elements, or base classes of an object.
- Derived objects (and most-derived objects) only make sense in the context of class inheritance.
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.
- Every complete object of class-type is a most-derived object only if that class inherits.
- A most-derived object may be a subobject, so you cannot infer object completeness from most-derivedness (however, you can infer that the most-derived object is of class type).
So if I have created an object of Base like this:
Base b_obj = Base()
, Is the objectb_obj
is "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_obj
is 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;
- It is an object of type
MostDerived
- It is an object of type
Derived
- It is an object of type
Base
- If it is not a member subobject, then it is a complete object
- Its most-derived object is of type
MostDerived
- It has a subobject of type
Derived
, which is neither a complete object nor a most-derived object - Its subobject of type
Derived
has a subobject of typeBase
, which is neither a complete object nor a most-derived object.