Where's the vtable?
Andrea 'fwyzard' Bocci
fwyzard@inwind.it
Mon Dec 30 07:02:00 GMT 2002
At 16.01 30/12/2002 +1300, John Carter wrote:
>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?
I think you can see it both as non-(pure virtual) function or as (non-pure)
virtual function, as there is no difference in the meaning: the Vtable is
generated if there is at least a function with a body (non pure virtual),
more or less, that needs to be called (non inline).
>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?
At least one that is NOT inline AND NOT pure virtual. And all the methods
that you call, of course.
>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.
If it's inline, it does not get you a vtable, as the paragraph you quoted says.
>It's a pity that the vtable is just a static variable like any other class
>static.
Sorry, I couldn't follow you here. What do you mean ?
fwyzard
More information about the Gcc-help
mailing list