<s>
In	O
computer	B-General_Concept
programming	I-General_Concept
,	O
a	O
virtual	B-Language
method	I-Language
table	I-Language
(	O
VMT	O
)	O
,	O
virtual	B-Language
function	I-Language
table	I-Language
,	O
virtual	O
call	O
table	O
,	O
dispatch	O
table	O
,	O
vtable	B-Language
,	O
or	O
vftable	B-Language
is	O
a	O
mechanism	O
used	O
in	O
a	O
programming	O
language	O
to	O
support	O
dynamic	O
dispatch	O
(	O
or	O
run-time	B-Library
method	B-Language
binding	O
)	O
.	O
</s>
<s>
Whenever	O
a	O
class	O
defines	O
a	O
virtual	B-Application
function	I-Application
(	O
or	O
method	B-Language
)	O
,	O
most	O
compilers	B-Language
add	O
a	O
hidden	O
member	B-Application
variable	I-Application
to	O
the	O
class	O
that	O
points	O
to	O
an	O
array	O
of	O
pointers	O
to	O
(	O
virtual	O
)	O
functions	O
called	O
the	O
virtual	B-Language
method	I-Language
table	I-Language
.	O
</s>
<s>
These	O
pointers	O
are	O
used	O
at	B-Library
runtime	I-Library
to	O
invoke	O
the	O
appropriate	O
function	O
implementations	O
,	O
because	O
at	O
compile	B-Application
time	I-Application
it	O
may	O
not	O
yet	O
be	O
known	O
if	O
the	O
base	O
function	O
is	O
to	O
be	O
called	O
or	O
a	O
derived	O
one	O
implemented	O
by	O
a	O
class	O
that	O
inherits	B-Language
from	O
the	O
base	O
class	O
.	O
</s>
<s>
There	O
are	O
many	O
different	O
ways	O
to	O
implement	O
such	O
dynamic	O
dispatch	O
,	O
but	O
use	O
of	O
virtual	B-Language
method	I-Language
tables	I-Language
is	O
especially	O
common	O
among	O
C++	B-Language
and	O
related	O
languages	O
(	O
such	O
as	O
D	B-Application
and	O
C#	B-Application
)	O
.	O
</s>
<s>
Languages	O
that	O
separate	O
the	O
programmatic	O
interface	O
of	O
objects	O
from	O
the	O
implementation	O
,	O
like	O
Visual	B-Language
Basic	I-Language
and	O
Delphi	B-Language
,	O
also	O
tend	O
to	O
use	O
this	O
approach	O
,	O
because	O
it	O
allows	O
objects	O
to	O
use	O
a	O
different	O
implementation	O
simply	O
by	O
using	O
a	O
different	O
set	O
of	O
method	B-Language
pointers	O
.	O
</s>
<s>
Suppose	O
a	O
program	O
contains	O
three	O
classes	O
in	O
an	O
inheritance	B-Language
hierarchy	I-Language
:	O
a	O
superclass	O
,	O
,	O
and	O
two	O
subclasses	O
,	O
and	O
.	O
</s>
<s>
Class	O
defines	O
a	O
virtual	B-Application
function	I-Application
named	O
,	O
so	O
its	O
subclasses	O
may	O
provide	O
an	O
appropriate	O
implementation	O
(	O
e.g.	O
</s>
<s>
The	O
class	O
cannot	O
generally	O
be	O
determined	O
statically	O
(	O
that	O
is	O
,	O
at	O
compile	B-Application
time	I-Application
)	O
,	O
so	O
neither	O
can	O
the	O
compiler	B-Language
decide	O
which	O
function	O
to	O
call	O
at	O
that	O
time	O
.	O
</s>
<s>
The	O
call	O
must	O
be	O
dispatched	O
to	O
the	O
right	O
function	O
dynamically	O
(	O
that	O
is	O
,	O
at	O
run	B-Library
time	I-Library
)	O
instead	O
.	O
</s>
<s>
An	O
object	O
's	O
virtual	B-Language
method	I-Language
table	I-Language
will	O
contain	O
the	O
addresses	B-General_Concept
of	O
the	O
object	O
's	O
dynamically	O
bound	O
methods	O
.	O
</s>
<s>
Method	B-Language
calls	I-Language
are	O
performed	O
by	O
fetching	O
the	O
method	B-Language
's	O
address	O
from	O
the	O
object	O
's	O
virtual	B-Language
method	I-Language
table	I-Language
.	O
</s>
<s>
The	O
virtual	B-Language
method	I-Language
table	I-Language
is	O
the	O
same	O
for	O
all	O
objects	O
belonging	O
to	O
the	O
same	O
class	O
,	O
and	O
is	O
therefore	O
typically	O
shared	O
between	O
them	O
.	O
</s>
<s>
Objects	O
belonging	O
to	O
type-compatible	O
classes	O
(	O
for	O
example	O
siblings	O
in	O
an	O
inheritance	B-Language
hierarchy	I-Language
)	O
will	O
have	O
virtual	B-Language
method	I-Language
tables	I-Language
with	O
the	O
same	O
layout	O
:	O
the	O
address	O
of	O
a	O
given	O
method	B-Language
will	O
appear	O
at	O
the	O
same	O
offset	O
for	O
all	O
type-compatible	O
classes	O
.	O
</s>
<s>
Thus	O
,	O
fetching	O
the	O
method	B-Language
's	O
address	O
from	O
a	O
given	O
offset	O
into	O
a	O
virtual	B-Language
method	I-Language
table	I-Language
will	O
get	O
the	O
method	B-Language
corresponding	O
to	O
the	O
object	O
's	O
actual	O
class	O
.	O
</s>
<s>
The	O
C++	B-Language
standards	O
do	O
not	O
mandate	O
exactly	O
how	O
dynamic	O
dispatch	O
must	O
be	O
implemented	O
,	O
but	O
compilers	B-Language
generally	O
use	O
minor	O
variations	O
on	O
the	O
same	O
basic	O
model	O
.	O
</s>
<s>
Typically	O
,	O
the	O
compiler	B-Language
creates	O
a	O
separate	O
virtual	B-Language
method	I-Language
table	I-Language
for	O
each	O
class	O
.	O
</s>
<s>
When	O
an	O
object	O
is	O
created	O
,	O
a	O
pointer	O
to	O
this	O
table	O
,	O
called	O
the	O
virtual	B-Language
table	I-Language
pointer	O
,	O
vpointer	B-Language
or	O
VPTR	O
,	O
is	O
added	O
as	O
a	O
hidden	O
member	O
of	O
this	O
object	O
.	O
</s>
<s>
As	O
such	O
,	O
the	O
compiler	B-Language
must	O
also	O
generate	O
"	O
hidden	O
"	O
code	O
in	O
the	O
constructors	O
of	O
each	O
class	O
to	O
initialize	O
a	O
new	O
object	O
's	O
virtual	B-Language
table	I-Language
pointer	O
to	O
the	O
address	O
of	O
its	O
class	O
's	O
virtual	B-Language
method	I-Language
table	I-Language
.	O
</s>
<s>
Many	O
compilers	B-Language
place	O
the	O
virtual	B-Language
table	I-Language
pointer	O
as	O
the	O
last	O
member	O
of	O
the	O
object	O
;	O
other	O
compilers	B-Language
place	O
it	O
as	O
the	O
first	O
;	O
portable	O
source	O
code	O
works	O
either	O
way	O
.	O
</s>
<s>
For	O
example	O
,	O
g++	B-Application
previously	O
placed	O
the	O
pointer	O
at	O
the	O
end	O
of	O
the	O
object	O
.	O
</s>
<s>
Consider	O
the	O
following	O
class	O
declarations	O
in	O
C++	B-Language
syntax	O
:	O
</s>
<s>
and	O
the	O
following	O
piece	O
of	O
C++	B-Language
code	I-Language
:	O
</s>
<s>
g++	B-Application
3.4.6	O
from	O
GCC	B-Application
produces	O
the	O
following	O
32-bit	O
memory	O
layout	O
for	O
the	O
object	O
b2	O
:	O
</s>
<s>
and	O
the	O
following	O
memory	O
layout	O
for	O
the	O
object	O
d	B-Application
:	O
</s>
<s>
Note	O
that	O
those	O
functions	O
not	O
carrying	O
the	O
keyword	O
virtual	O
in	O
their	O
declaration	O
(	O
such	O
as	O
fnonvirtual( )	O
and	O
d( )	O
)	O
do	O
not	O
generally	O
appear	O
in	O
the	O
virtual	B-Language
method	I-Language
table	I-Language
.	O
</s>
<s>
They	O
are	O
necessary	O
to	O
ensure	O
delete	O
d	B-Application
can	O
free	O
up	O
memory	O
not	O
just	O
for	O
D	B-Application
,	O
but	O
also	O
for	O
B1	O
and	O
B2	O
,	O
if	O
d	B-Application
is	O
a	O
pointer	O
or	O
reference	O
to	O
the	O
types	O
B1	O
or	O
B2	O
.	O
</s>
<s>
Overriding	B-Language
of	I-Language
the	I-Language
method	I-Language
f2( )	O
in	O
class	O
D	B-Application
is	O
implemented	O
by	O
duplicating	O
the	O
virtual	B-Language
method	I-Language
table	I-Language
of	O
B2	O
and	O
replacing	O
the	O
pointer	O
to	O
B2::f2( )	O
with	O
a	O
pointer	O
to	O
D::f2( )	O
.	O
</s>
<s>
The	O
g++	B-Application
compiler	B-Language
implements	O
the	O
multiple	B-Application
inheritance	I-Application
of	O
the	O
classes	O
B1	O
and	O
B2	O
in	O
class	O
D	B-Application
using	O
two	O
virtual	B-Language
method	I-Language
tables	I-Language
,	O
one	O
for	O
each	O
base	O
class	O
.	O
</s>
<s>
(	O
There	O
are	O
other	O
ways	O
to	O
implement	O
multiple	B-Application
inheritance	I-Application
,	O
but	O
this	O
is	O
the	O
most	O
common	O
.	O
)	O
</s>
<s>
This	O
leads	O
to	O
the	O
necessity	O
for	O
"	O
pointer	B-Application
fixups	I-Application
"	O
,	O
also	O
called	O
thunks	B-Application
,	O
when	O
casting	O
.	O
</s>
<s>
Consider	O
the	O
following	O
C++	B-Language
code	I-Language
:	O
</s>
<s>
While	O
d	B-Application
and	O
b1	O
will	O
point	O
to	O
the	O
same	O
memory	B-General_Concept
location	I-General_Concept
after	O
execution	O
of	O
this	O
code	O
,	O
b2	O
will	O
point	O
to	O
the	O
location	O
d+8	O
(	O
eight	O
bytes	O
beyond	O
the	O
memory	B-General_Concept
location	I-General_Concept
of	O
d	B-Application
)	O
.	O
</s>
<s>
Thus	O
,	O
b2	O
points	O
to	O
the	O
region	O
within	O
d	B-Application
that	O
"	O
looks	O
like	O
"	O
an	O
instance	O
of	O
B2	O
,	O
i.e.	O
,	O
has	O
the	O
same	O
memory	O
layout	O
as	O
an	O
instance	O
of	O
B2	O
.	O
</s>
<s>
A	O
call	O
to	O
d->f1( )	O
is	O
handled	O
by	O
dereferencing	O
d	B-Application
's	O
D	B-Application
:	O
:B1	O
vpointer	B-Language
,	O
looking	O
up	O
the	O
f1	O
entry	O
in	O
the	O
virtual	B-Language
method	I-Language
table	I-Language
,	O
and	O
then	O
dereferencing	O
that	O
pointer	O
to	O
call	O
the	O
code	O
.	O
</s>
<s>
In	O
the	O
case	O
of	O
single	O
inheritance	B-Language
(	O
or	O
in	O
a	O
language	O
with	O
only	O
single	O
inheritance	B-Language
)	O
,	O
if	O
the	O
vpointer	B-Language
is	O
always	O
the	O
first	O
element	O
in	O
d	B-Application
(	O
as	O
it	O
is	O
with	O
many	O
compilers	B-Language
)	O
,	O
this	O
reduces	O
to	O
the	O
following	O
pseudo-C	O
++	O
:	O
</s>
<s>
Where	O
*	O
d	B-Application
refers	O
to	O
the	O
virtual	B-Language
method	I-Language
table	I-Language
of	O
D	B-Application
and	O
 [ 0 ] 	O
refers	O
to	O
the	O
first	O
method	B-Language
in	O
the	O
virtual	B-Language
method	I-Language
table	I-Language
.	O
</s>
<s>
The	O
parameter	O
d	B-Application
becomes	O
the	O
"	O
this	O
"	O
pointer	O
to	O
the	O
object	O
.	O
</s>
<s>
In	O
the	O
more	O
general	O
case	O
,	O
calling	O
B1::f1( )	O
or	O
D::f2( )	O
is	O
more	O
complicated	O
:	O
</s>
<s>
The	O
call	O
to	O
d->f1( )	O
passes	O
a	O
B1	O
pointer	O
as	O
a	O
parameter	O
.	O
</s>
<s>
The	O
call	O
to	O
d->f2( )	O
passes	O
a	O
B2	O
pointer	O
as	O
a	O
parameter	O
.	O
</s>
<s>
The	O
location	O
of	O
B2::f2	O
is	O
not	O
in	O
the	O
virtual	B-Language
method	I-Language
table	I-Language
for	O
D	B-Application
.	O
</s>
<s>
By	O
comparison	O
,	O
a	O
call	O
to	O
d->fnonvirtual( )	O
is	O
much	O
simpler	O
:	O
</s>
<s>
A	O
virtual	O
call	O
requires	O
at	O
least	O
an	O
extra	O
indexed	O
dereference	O
and	O
sometimes	O
a	O
"	O
fixup	O
"	O
addition	O
,	O
compared	O
to	O
a	O
non-virtual	O
call	O
,	O
which	O
is	O
simply	O
a	O
jump	O
to	O
a	O
compiled-in	O
pointer	O
.	O
</s>
<s>
Therefore	O
,	O
calling	O
virtual	B-Application
functions	I-Application
is	O
inherently	O
slower	O
than	O
calling	O
non-virtual	O
functions	O
.	O
</s>
<s>
An	O
experiment	O
done	O
in	O
1996	O
indicates	O
that	O
approximately	O
6	O
–	O
13%	O
of	O
execution	B-Library
time	I-Library
is	O
spent	O
simply	O
dispatching	O
to	O
the	O
correct	O
function	O
,	O
though	O
the	O
overhead	O
can	O
be	O
as	O
high	O
as	O
50%	O
.	O
</s>
<s>
The	O
cost	O
of	O
virtual	B-Application
functions	I-Application
may	O
not	O
be	O
so	O
high	O
on	O
modern	O
architectures	O
due	O
to	O
much	O
larger	O
caches	O
and	O
better	O
branch	B-General_Concept
prediction	I-General_Concept
.	O
</s>
<s>
Furthermore	O
,	O
in	O
environments	O
where	O
JIT	O
compilation	B-Language
is	O
not	O
in	O
use	O
,	O
virtual	B-Application
function	I-Application
calls	O
usually	O
cannot	O
be	O
inlined	O
.	O
</s>
<s>
In	O
certain	O
cases	O
it	O
may	O
be	O
possible	O
for	O
the	O
compiler	B-Language
to	O
perform	O
a	O
process	O
known	O
as	O
devirtualization	O
in	O
which	O
,	O
for	O
instance	O
,	O
the	O
lookup	O
and	O
indirect	O
call	O
are	O
replaced	O
with	O
a	O
conditional	O
execution	O
of	O
each	O
inlined	O
body	O
,	O
but	O
such	O
optimizations	O
are	O
not	O
common	O
.	O
</s>
<s>
To	O
avoid	O
this	O
overhead	O
,	O
compilers	B-Language
usually	O
avoid	O
using	O
virtual	B-Language
method	I-Language
tables	I-Language
whenever	O
the	O
call	O
can	O
be	O
resolved	O
at	O
compile	B-Application
time	I-Application
.	O
</s>
<s>
Thus	O
,	O
the	O
call	O
to	O
f1	O
above	O
may	O
not	O
require	O
a	O
table	O
lookup	O
because	O
the	O
compiler	B-Language
may	O
be	O
able	O
to	O
tell	O
that	O
d	B-Application
can	O
only	O
hold	O
a	O
D	B-Application
at	O
this	O
point	O
,	O
and	O
D	B-Application
does	O
not	O
override	B-Language
f1	O
.	O
</s>
<s>
Or	O
the	O
compiler	B-Language
(	O
or	O
optimizer	O
)	O
may	O
be	O
able	O
to	O
detect	O
that	O
there	O
are	O
no	O
subclasses	O
of	O
B1	O
anywhere	O
in	O
the	O
program	O
that	O
override	B-Language
f1	O
.	O
</s>
<s>
The	O
virtual	B-Language
method	I-Language
table	I-Language
is	O
generally	O
a	O
good	O
performance	O
trade-off	O
to	O
achieve	O
dynamic	O
dispatch	O
,	O
but	O
there	O
are	O
alternatives	O
,	O
such	O
as	O
binary	O
tree	O
dispatch	O
,	O
with	O
higher	O
performance	O
but	O
different	O
costs	O
.	O
</s>
<s>
However	O
,	O
virtual	B-Language
method	I-Language
tables	I-Language
only	O
allow	O
for	O
single	O
dispatch	O
on	O
the	O
special	O
"	O
this	O
"	O
parameter	O
,	O
in	O
contrast	O
to	O
multiple	O
dispatch	O
(	O
as	O
in	O
CLOS	B-Application
,	O
Dylan	B-Language
,	O
or	O
Julia	B-Application
)	O
,	O
where	O
the	O
types	O
of	O
all	O
parameters	O
can	O
be	O
taken	O
into	O
account	O
in	O
dispatching	O
.	O
</s>
<s>
Virtual	B-Language
method	I-Language
tables	I-Language
also	O
only	O
work	O
if	O
dispatching	O
is	O
constrained	O
to	O
a	O
known	O
set	O
of	O
methods	O
,	O
so	O
they	O
can	O
be	O
placed	O
in	O
a	O
simple	O
array	O
built	O
at	O
compile	B-Application
time	I-Application
,	O
in	O
contrast	O
to	O
duck	B-Application
typing	I-Application
languages	O
(	O
such	O
as	O
Smalltalk	B-Application
,	O
Python	B-Language
or	O
JavaScript	B-Language
)	O
.	O
</s>
<s>
Languages	O
that	O
provide	O
either	O
or	O
both	O
of	O
these	O
features	O
often	O
dispatch	O
by	O
looking	O
up	O
a	O
string	O
in	O
a	O
hash	B-Algorithm
table	I-Algorithm
,	O
or	O
some	O
other	O
equivalent	O
method	B-Language
.	O
</s>
<s>
There	O
are	O
a	O
variety	O
of	O
techniques	O
to	O
make	O
this	O
faster	O
(	O
e.g.	O
,	O
interning/tokenizing	O
method	B-Language
names	I-Language
,	O
caching	O
lookups	O
,	O
just-in-time	O
compilation	B-Language
)	O
.	O
</s>
