Where's the vtable?

John Carter john.carter@tait.co.nz
Mon Dec 30 04:09:00 GMT 2002


The manual utters these mystic words....
===========================================================================
VTables
    C++ virtual functions are implemented in most compilers using a lookup
table, known as a vtable. The vtable contains pointers to the virtual
functions provided by a class, and each object of the class contains a
pointer to its vtable (or vtables, in some multiple-inheritance
situations). If the class declares any non-inline, non-pure virtual
functions, the first one is chosen as the "key method" for the class, and
the vtable is only emitted in the translation unit where the key method is
defined.

    Note: If the chosen key method is later defined as inline, the vtable
will still be emitted in every translation unit which defines it. Make
sure that any inline virtuals are declared inline in the class body, even
if they are not defined there.
==========================================================================

Question 1. In the first paragraph, how far does the "non" in "non-pure
virtual function" go? Virtual function that is non-pure? Function that is
non-virtual?

Question 2. I'm using MockObject's to test my program.

ie. I '#include "MyClass.H"' my class header into MockClass.C and then
define just those methods I'm using for testing. Unfortunately sometimes
this means I get "undefined reference to `vtable for MyClass'" when
linking.

How can I tell which method must I define to get the vtable? Doing the
obvious defining the first function or first virtual function that is
non-pure doesn't seem to help. Perhaps because it is inline? I don't know.

It's a pity that the vtable is just a static variable like any other class
static.


John Carter                             Phone : (64)(3) 358 6639
Tait Electronics                        Fax   : (64)(3) 359 4632
PO Box 1645 Christchurch                Email : john.carter@tait.co.nz
New Zealand

Good Ideas:
Ruby                 - http://www.ruby-lang-org - The best of perl,python,scheme without the pain.
Valgrind             - http://developer.kde.org/~sewardj/ - memory debugger for x86-GNU/Linux
Free your books      - http://www.bookcrossing.com
Memtest86            - http://www.memtest86.com - Free standalone, excellent memory tester for PC's.



More information about the Gcc-help mailing list