public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Where's the vtable?
@ 2002-12-30  4:09 John Carter
  0 siblings, 0 replies; 2+ messages in thread
From: John Carter @ 2002-12-30  4:09 UTC (permalink / raw)
  To: gcc-help

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.

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Where's the vtable?
       [not found] <Pine.LNX.4.50.0212301555170.24193-100000@localhost.localdo main>
@ 2002-12-30  7:02 ` Andrea 'fwyzard' Bocci
  0 siblings, 0 replies; 2+ messages in thread
From: Andrea 'fwyzard' Bocci @ 2002-12-30  7:02 UTC (permalink / raw)
  To: John Carter; +Cc: gcc-help

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 


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2002-12-30 12:15 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-12-30  4:09 Where's the vtable? John Carter
     [not found] <Pine.LNX.4.50.0212301555170.24193-100000@localhost.localdo main>
2002-12-30  7:02 ` Andrea 'fwyzard' Bocci

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).