Re: vtbl of the subobject to the complete object

From: Ron Natalie <ron_at_sensor.com>
Date: 14 Nov 2004 11:57:36 -0500


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. 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.

>
> 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! ]
Received on Sun Nov 14 2004 - 08:53:55 PST

Click to report inappropriate content