Topic: Complete object" and "most derived object


Author: "msalters" <Michiel.Salters@logicacmg.com>
Date: Tue, 14 Dec 2004 15:06:18 CST
Raw View
dragoncoder wrote:
> Hello experts,
>
> I am newbie in C++ (not exactly a newbie) :-)
>
> While going through the standard section 1.7/2-3 it says,
>
> Objects can contain other objects, called subobjects.
> A subobject
> - can be a member subobject,
> - a base class subobject
> or an array element.
> An object that is not a subobject of any other object is called a
> complete object.
>
> 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.
> If a complete object, a data member, or an array element is of class
> type, its type is considered the most derived class, to distinguish
it
> from the class type of any base class subobject; an object of a most
> derived class type is called a most derived object.
>
> What I could understand from the definition of complete object is
that
> it is the top-most level object which is not a part of any other
object
> (either by derivation or by composition). Is my understanding right?

Yes.

> The second part which talks about most derived class/object is
> confusing me. Can you please give an example of what the most derived
> class/onject means?

Well, if we look at a subobject, we see a chain of containing
objects up to its complete object. This chain can contain "is a
base subobject" or "is a member subobject" links. If we follow
only "is a base subobject" links, we can end up with either
some member subobject or the complete object

e.g.

class Base {};
class A : public Base {};
class B : public A {};
class C { public: B b; }

C complete;
A* ptr = &complete.b;
Now, the ptr points to a subobject. The complete object of *ptr
is complete, but the most derived object of *ptr is the b
member subobject in complete.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: Alberto Barbati <AlbertoBarbati@libero.it>
Date: Tue, 14 Dec 2004 15:06:40 CST
Raw View
dragoncoder wrote:
>
> The second part which talks about most derived class/object is
> confusing me. Can you please give an example of what the most derived
> class/onject means?
>

I agree that wording is bit obscure, but I also have to acknowledge that
it's difficult to say it in words... Perhaps it would have been better
to reverse the definitions and define the most derived object first and
the most derived class later. Let's try it:

First case: complete objects, data member and array elements
   The _most derived object_ is the object itself

Second case: base class subobjects
   The _most derived object_ is the complete object (which is different
from the object itself)

The _most derived class_ is the type of the most derived object. In the
first case it's the type of the object, in the second case its the type
of the complete object (and always differs from the type of the object).

For example:

   class Element {};
   class Derived : public Base
   {
     Element m;
   };

   Derived c;
   Element a[10];

c has a base class subobject of type Base, but there's no actual C++
notation to refer to such suboject, so we call it "b" in the scheme below:

Object |  Type   |Compl. Object| M.D. Object | M.D. Class
-------+---------+-------------+-------------+---------------
   c    | Derived |      c      |    c        |   Derived
   "b"  |  Base   |      c      |    c        |   Derived
   c.m  | Element |      c      |    c.m      |   Element
   a[0] | Element |      a      |    a[0]     |   Element

Hope it helps,

Alberto

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: alfps@start.no (Alf P. Steinbach)
Date: Tue, 14 Dec 2004 23:20:08 GMT
Raw View
* dragoncoder:
> Hello experts,
>
> I am newbie in C++ (not exactly a newbie) :-)
>
> While going through the standard section 1.7/2-3 it says,
>
> Objects can contain other objects, called subobjects.
> A subobject
> - can be a member subobject,
> - a base class subobject
> or an array element.
> An object that is not a subobject of any other object is called a
> complete object.
>
> 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.
> If a complete object, a data member, or an array element is of class
> type, its type is considered the most derived class, to distinguish it
> from the class type of any base class subobject; an object of a most
> derived class type is called a most derived object.
>
> What I could understand from the definition of complete object is that
> it is the top-most level object which is not a part of any other object
> (either by derivation or by composition). Is my understanding right?

No.  The standard here differentiates between "complete object", defined
as "not a subobject", and "complete object of x", which is the top-most
level object containing x.


> The second part which talks about most derived class/object is
> confusing me. Can you please give an example of what the most derived
> class/onject means?

Essentially it means the type T used to instantiate the object.  If T
is a derived class, say, of class Base, and you have an object of most
derived type T, then Base is also type for o but not a most derived one.
When o is regarded as type Base you are referring to the only the Base
subobject of o.  For example

   struct Base{ int x; Base(): x(1) {} };
   struct T{ int y; T(): y(2) {} };

   int main()
   {
       T  o;   // T is the most derived type of complete object o.
       Base& oRef = o;   // Base is a type but not the most derived.
       Base o2 = o;   // Here we copy only the Base sub-object, slicing.
   }

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: ron@sensor.com (Ron Natalie)
Date: Tue, 14 Dec 2004 23:20:28 GMT
Raw View
dragoncoder wrote:

>
> The second part which talks about most derived class/object is
> confusing me. Can you please give an example of what the most derived
> class/onject means?
>

class A {
     virtual v() { }
};

class B : public A {
  //...
};

class C {
   // ...
};

class D : public B {
     virtual v() { }
     C  c;
} d;

For the object d, d is the complete object, within it are the C subobject
(member called c) and the base class A and B parts of d.

The class D is the most derived class.   This is used for virtual functions
primarily.   When calling v() in A's context it has to consider virtual functions
of the same signature in more derived class and finds the one in D, the most
derived class.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: "dragoncoder" <pktiwary@gmail.com>
Date: Tue, 14 Dec 2004 02:02:37 CST
Raw View
Hello experts,

I am newbie in C++ (not exactly a newbie) :-)

While going through the standard section 1.7/2-3 it says,

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

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.
If a complete object, a data member, or an array element is of class
type, its type is considered the most derived class, to distinguish it
from the class type of any base class subobject; an object of a most
derived class type is called a most derived object.

What I could understand from the definition of complete object is that
it is the top-most level object which is not a part of any other object
(either by derivation or by composition). Is my understanding right?

The second part which talks about most derived class/object is
confusing me. Can you please give an example of what the most derived
class/onject means?

Thanks a tons.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]