Re: vtbl of the subobject to the complete object
From: Ben Hutchings <ben-public-nospam_at_decadentplace.org.uk>
Date: 3 Dec 2004 09:39:51 -0500 Marlene Miller wrote: > 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.
> 6. Does this imply, if we invoke a virtual function of Ival using pb that is No. In the implementations I know of, the vtable for a Ival_box subobject 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! ]
Received on Fri Dec 03 2004 - 06:36:11 PST
|
Click to report inappropriate content