This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Where's the vtable?
- From: John Carter <john dot carter at tait dot co dot nz>
- To: gcc-help at gcc dot gnu dot org
- Date: Mon, 30 Dec 2002 16:01:57 +1300 (NZDT)
- Subject: Where's the vtable?
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.