home *** CD-ROM | disk | FTP | other *** search
- Xref: sparky comp.lang.c++:11259 comp.std.c++:902
- Newsgroups: comp.lang.c++,comp.std.c++
- Path: sparky!uunet!caen!hellgate.utah.edu!asylum.cs.utah.edu!clark
- From: clark%asylum.cs.utah.edu@cs.utah.edu (Charles Clark)
- Subject: Re: Language extensions for run-time type identification
- Date: 21 Jul 92 13:55:37 MDT
- Message-ID: <1992Jul21.135537.23421@hellgate.utah.edu>
- Followup-To: comp.lang.c++,comp.std.c++
- Sender: clark@cs.utah.edu
- Organization: University of Utah CS Dept
- References: <14ft2uINNjh4@agate.berkeley.edu> <1992Jul21.143131.6902@cadsun.corp.mot.com>
- Lines: 42
-
- In article <1992Jul21.143131.6902@cadsun.corp.mot.com> shang@corp.mot.com writes:
- >In article <14ft2uINNjh4@agate.berkeley.edu> jbuck@forney.berkeley.edu (Joe
- >Buck) writes:
- >> * the fact that it is trivial to force any class for which run-time type
- >> identification is needed to have a virtual function table (simply add
- >> a virtual destructor).
- >>
- >Adding something virtual just for the purpose that has nothing to do with it?
-
- I agree, forcing the programmer to add a virtual destructor (or any virtual
- function) to the design of his/her classes (so the resulting vtable can
- be used for type id) is dubious. Unfortunately, a programmer without access
- to a a C++ compiler which provides run-time type id can do no better.
-
- >> In most implementations of classes with virtual functions, each object
- >> already has, in effect, a type-ID: the pointer to the virtual function
- >> table. This is a natural hook on which to hang run time type checking.
- >>
- >The pointer to vtable can not work as a type-ID for a language with multiple
- >inheritance. An object of particular class derived from multiple bases may
- >contain serveral different vtable references. Different base pointers to the
- >same object may use different vtable pointers.
-
- Indeed. The above scheme fails when vtables are shared between base and
- derived classes. This sharing occurs when using multiple inheritance
- (or virtual base classes) and no inherited methods from one of the base
- classes are redefined. A cast to that base, and reference to the vtable, will
- yield the (shared) base vtable. Ooops, wrong type id...
-
- Note that this is a compiler implementation detail -- an optimization that
- C++ compilers use to reduce the number of vtables generated. One could
- modify the compiler to not make this optimization and generate fresh vtables
- for the base portions of derived classes. Now, if you've got your heart set
- on run-time type id and you're prepared to modify the compiler, there is
- no point in forcing the programmer to write useless virtual functions so
- that a vtable will be generated. The compiler can be modified to always
- generate a vtable (although it may sometimes be empty).
-
- The overhead of doing this is one word per instance of a class that would
- not normally have a vtable (plus the extra vtables).
-
- --Charles (clark@cs.utah.edu)
-