C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

vtbl of the subobject to the complete object

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Marlene Miller
Guest





PostPosted: Fri Nov 12, 2004 11:55 am    Post subject: vtbl of the subobject to the complete object Reply with quote



I am confused about the offset to the complete object in the vtbl of the
subobject .

----
Re: The C++ Programming Language (Special Edition) 15.4.1 page 409-410.

class My_slider : public Ival_slider {
// polymorphic base (Ival_slider has virtual functions)
}

void g(Ival_box* pb)
{
My_slider* pd1 = dynamic_cast<My_slider*>(pb); //ok
}

There are two boxes labeled My_slider and vtbl. My_slider has an attribute
vptr. There is a dotted arrow from the My_slider box to the vtbl box and a
dashed arrow from the vtbl box to the My_slider box.

"The dashed arrow represents an offset that allows the start of the complete
object to be found given only a pointer to a polymorphic sub-object."

----

1. Is "a pointer to a polymorphic sub-object" referring to pb?

2. Is "sub-object" referring to the base class sub-object of the My_slider
object?

3. Is "the complete object" referring to the My_slider object?

4. Are they saying pb1 is a pointer to the My_slider object and not a
pointer to the Ival sub-object?

5. Are they saying given pb, we can navigate to the vtbl of the base class
sub-object Ival, then navigate to the complete object My_slider?

6. Does this imply, if we invoke a virtual function of Ival using pb that is
overridden in My_slider, pb points to the base class sub-object, we navigate
to the vtbl of this sub-object, then we navigate to the My_slider complete
object, then we navigate to the vtbl of the My_slider object to get the
implementation of the function?

Regards, Marlene


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Ron Natalie
Guest





PostPosted: Fri Nov 12, 2004 3:45 pm    Post subject: Re: vtbl of the subobject to the complete object Reply with quote



Marlene Miller wrote:
Quote:
I am confused about the offset to the complete object in the vtbl of the
subobject

Really, you shouldn't even care. All you need to do is follow the rules
for well-defined conversions and this all takes place automatically. These
authors try to insert some real life implementation issues to explain why
you have to do it, but whatever they say is at best impelementation specific.
Quote:

----
Re: The C++ Programming Language (Special Edition) 15.4.1 page 409-410.

class My_slider : public Ival_slider {
// polymorphic base (Ival_slider has virtual functions)
}

void g(Ival_box* pb)
{
My_slider* pd1 = dynamic_cast<My_slider*>(pb); //ok
}

There are two boxes labeled My_slider and vtbl. My_slider has an attribute
vptr. There is a dotted arrow from the My_slider box to the vtbl box and a
dashed arrow from the vtbl box to the My_slider box.

"The dashed arrow represents an offset that allows the start of the complete
object to be found given only a pointer to a polymorphic sub-object."

----

1. Is "a pointer to a polymorphic sub-object" referring to pb?

I don't have the book handy so I'll assume Ival_slider somehow inherits
from Ival_box (and that Ival_box itself is a polymorphic class), in that
case, yes.

Quote:
2. Is "sub-object" referring to the base class sub-object of the My_slider
object?
Yes, sub-object either means base class or non-static member, but it's pretty

clear this example means a base class (not necessarily a direct one, though).

Quote:

3. Is "the complete object" referring to the My_slider object?

Yes, a complete object is one that is not part of any other.

Quote:

4. Are they saying pb1 is a pointer to the My_slider object and not a
pointer to the Ival sub-object?

Exactly, dynamic_cast either finds that the pb is a subobject of a My_slider
tyhpe object and converts the pointer to refer to the My_slider object, or it
returns a null pointer.

Quote:

5. Are they saying given pb, we can navigate to the vtbl of the base class
sub-object Ival, then navigate to the complete object My_slider?

Yes, provided all the types that are operands of dynamic_cast are:
1. Polymorphic classes themselves (i.e., have virtual functions)
2. Are accessible in the converted to type (i.e., no private inheritance)
Quote:

6. Does this imply, if we invoke a virtual function of Ival using pb that is
overridden in My_slider, pb points to the base class sub-object, we navigate
to the vtbl of this sub-object, then we navigate to the My_slider complete
object, then we navigate to the vtbl of the My_slider object to get the
implementation of the function?

Yes...the most derived overrider is called. That's what the language requires
and the vtbl is a common way to make that happen. In addition to having entries
which are effectively the function poitner for the virtual function, the vtbl
also will contain whatever information (offsets, etc..) necessary to convert
the subobject poitner into the derived object so that the "this" poitner is
correct inside the function.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Marlene Miller
Guest





PostPosted: Sat Nov 13, 2004 2:45 am    Post subject: Re: vtbl of the subobject to the complete object Reply with quote



Thank you Ron for your help, your insights and your time. A few remarks
below.

"Ron Natalie" <ron (AT) sensor (DOT) com> wrote

Quote:
Really, you shouldn't even care. All you need to do is follow the rules
for well-defined conversions and this all takes place automatically.
These
authors try to insert some real life implementation issues to explain why
you have to do it, but whatever they say is at best impelementation
specific.


Well okay. I did wonder why the base class has to have virtual functions for
a dynamic_cast to work. Now I have a reason instead of a rule to remember.

Quote:
1. Is "a pointer to a polymorphic sub-object" referring to pb?

I don't have the book handy so I'll assume Ival_slider somehow inherits
from Ival_box (and that Ival_box itself is a polymorphic class), in that
case, yes.

Oops. I thought I had included all the relevant context.

class Ival_box { ... }
class Ival_slider : public Ival_box { ... } // has virtual functions
class My_slider : public Ival_slider { ... }

Quote:
3. Is "the complete object" referring to the My_slider object?

Yes, a complete object is one that is not part of any other.

I didn't realize "complete object" is an official technical term. Now, I
found it defined in the standard.

It's a new idea for me, that the base class object is *contained in* the
derived object. I imagined the derived class object to be one blob with
values of both classes.

In the Java Language Specification 12.5 "Whenever a new class instance is
created, memory space is allocated for it with room for all the instance
variables declared in the class type and all the instance variables declared
in each superclass of the class type, including all the instance variables
that may be hidden." For them, sub-object is not a relevant concept.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Ron Natalie
Guest





PostPosted: Sun Nov 14, 2004 4:57 pm    Post subject: Re: vtbl of the subobject to the complete object Reply with quote

Marlene Miller wrote:

Quote:
Well okay. I did wonder why the base class has to have virtual functions for
a dynamic_cast to work. Now I have a reason instead of a rule to remember.

The major reason is that without virtual fucntions there isn't the "vtbl"
(or whatever typing baggage the implementer might use instead). C++ uses
the "don't generate overhead for what you don't use" paradigm. The idea
is that virtual functions signal polymorphic behavior (pretty hard to
not use a polymorphic system without them) so that also is what the support
for dynamic_cast and dynamic typeid depends on.


Quote:

It's a new idea for me, that the base class object is *contained in* the
derived object. I imagined the derived class object to be one blob with
values of both classes.

Well it's one blob that contains the subobjects intact (at least for those
without private inheritance). This is because C++ lets you convert pointers
to any subobject and then treat the subobject as if it were standalone (until
virtual functions, etc...) get invoked.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Ben Hutchings
Guest





PostPosted: Fri Dec 03, 2004 2:33 pm    Post subject: Re: vtbl of the subobject to the complete object Reply with quote

Ron Natalie wrote:
Quote:
Marlene Miller wrote:

Well okay. I did wonder why the base class has to have virtual functions for
a dynamic_cast to work. Now I have a reason instead of a rule to remember.

The major reason is that without virtual fucntions there isn't the "vtbl"
(or whatever typing baggage the implementer might use instead). C++ uses
the "don't generate overhead for what you don't use" paradigm.
snip


Another major reason would be that adding a vtable pointer (or other
data supporting RTTI) to every struct would break binary compatibility
with C code that uses the same struct declarations.

Besides which, dynamic_cast doesn't even seem to be useful for types
without virtual functions. If you're not using dynamic polymorphism
then presumably you can use static_cast if for some reason you have a
pointer or reference to a base sub-object and need to get a derived
object.

--
Ben Hutchings
Beware of bugs in the above code;
I have only proved it correct, not tried it. - Donald Knuth

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Ben Hutchings
Guest





PostPosted: Fri Dec 03, 2004 2:39 pm    Post subject: Re: vtbl of the subobject to the complete object Reply with quote

Marlene Miller wrote:
Quote:
I am confused about the offset to the complete object in the vtbl of the
subobject .

----
Re: The C++ Programming Language (Special Edition) 15.4.1 page 409-410.

class My_slider : public Ival_slider {
// polymorphic base (Ival_slider has virtual functions)
}

void g(Ival_box* pb)
{
My_slider* pd1 = dynamic_cast<My_slider*>(pb); //ok
}

There are two boxes labeled My_slider and vtbl. My_slider has an attribute
vptr. There is a dotted arrow from the My_slider box to the vtbl box and a
dashed arrow from the vtbl box to the My_slider box.

"The dashed arrow represents an offset that allows the start of the complete
object to be found given only a pointer to a polymorphic sub-object."
snip
5. Are they saying given pb, we can navigate to the vtbl of the base class
sub-object Ival, then navigate to the complete object My_slider?

That's one way an implementer could arrange this.

However, since Ival_box is a non-virtual base of My_slider, the
Ival_box sub-object of a My_slider object is likely to be at a
constant offset within it, so there should be no need for the
generated code to use a vtable at all.

Quote:
6. Does this imply, if we invoke a virtual function of Ival using pb that is
overridden in My_slider, pb points to the base class sub-object, we navigate
to the vtbl of this sub-object, then we navigate to the My_slider complete
object, then we navigate to the vtbl of the My_slider object to get the
implementation of the function?

No. In the implementations I know of, the vtable for a Ival_box sub-
object in a My_slider would not be the same as the vtable for an
Ival_box complete object, and would contain the addresses of
overriding functions. In the implementation described in the book,
which has offset-to-complete-object in the vtable, clearly the vtable
for an Ival_box sub-object depends on the complete object's class, so
could be expected to contain pointers to overriding functions. So
there would be no need to look in a second vtable to find these.

To reiterate what Ron Natalie said, you don't need to understand
exactly how polymorphism is implemented, and should be aware that
the details can and do vary between implementations.

--
Ben Hutchings
Beware of bugs in the above code;
I have only proved it correct, not tried it. - Donald Knuth

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.