<s>
Virtual	B-Application
inheritance	I-Application
is	O
a	O
C++	B-Language
technique	O
that	O
ensures	O
only	O
one	O
copy	O
of	O
a	O
base	O
classs	O
member	O
variables	O
are	O
inherited	B-Language
by	O
grandchild	O
derived	O
classes	O
.	O
</s>
<s>
Without	O
virtual	B-Application
inheritance	I-Application
,	O
if	O
two	O
classes	O
B	O
and	O
C	O
inherit	O
from	O
a	O
class	O
A	O
,	O
and	O
a	O
class	O
D	O
inherits	O
from	O
both	O
B	O
and	O
C	O
,	O
then	O
D	O
will	O
contain	O
two	O
copies	O
of	O
As	O
member	O
variables	O
:	O
one	O
via	O
B	O
,	O
and	O
one	O
via	O
C	O
.	O
These	O
will	O
be	O
accessible	O
independently	O
,	O
using	O
scope	B-Language
resolution	I-Language
.	O
</s>
<s>
This	O
feature	O
is	O
most	O
useful	O
for	O
multiple	B-Application
inheritance	I-Application
,	O
as	O
it	O
makes	O
the	O
virtual	O
base	O
a	O
common	O
subobject	O
for	O
the	O
deriving	O
class	O
and	O
all	O
classes	O
that	O
are	O
derived	O
from	O
it	O
.	O
</s>
<s>
This	O
can	O
be	O
used	O
to	O
avoid	O
the	O
diamond	O
problem	O
by	O
clarifying	O
ambiguity	O
over	O
which	O
ancestor	O
class	O
to	O
use	O
,	O
as	O
from	O
the	O
perspective	O
of	O
the	O
deriving	O
class	O
(	O
D	O
in	O
the	O
example	O
above	O
)	O
the	O
virtual	O
base	O
(	O
A	O
)	O
acts	O
as	O
though	O
it	O
were	O
the	O
direct	O
base	B-Application
class	I-Application
of	O
D	O
,	O
not	O
a	O
class	O
derived	O
indirectly	O
through	O
a	O
base	O
(	O
B	O
or	O
C	O
)	O
.	O
</s>
<s>
It	O
is	O
used	O
when	O
inheritance	B-Language
represents	O
restriction	O
of	O
a	O
set	O
rather	O
than	O
composition	O
of	O
parts	O
.	O
</s>
<s>
In	O
C++	B-Language
,	O
a	O
base	B-Application
class	I-Application
intended	O
to	O
be	O
common	O
throughout	O
the	O
hierarchy	O
is	O
denoted	O
as	O
virtual	O
with	O
the	O
virtual	O
keyword	O
.	O
</s>
<s>
As	O
declared	O
above	O
,	O
a	O
call	O
to	O
bat.Eat	O
is	O
ambiguous	O
because	O
there	O
are	O
two	O
Animal	O
(	O
indirect	O
)	O
base	O
classes	O
in	O
Bat	O
,	O
so	O
any	O
Bat	O
object	O
has	O
two	O
different	O
Animal	O
base	B-Application
class	I-Application
subobjects	O
.	O
</s>
<s>
To	O
disambiguate	O
,	O
one	O
would	O
have	O
to	O
explicitly	O
convert	O
bat	O
to	O
either	O
base	B-Application
class	I-Application
subobject	O
:	O
</s>
<s>
In	O
this	O
case	O
,	O
the	O
double	O
inheritance	B-Language
of	O
Animal	O
is	O
probably	O
unwanted	O
,	O
as	O
we	O
want	O
to	O
model	O
that	O
the	O
relation	O
(	O
Bat	O
is	O
an	O
Animal	O
)	O
exists	O
only	O
once	O
;	O
that	O
a	O
Bat	O
is	O
a	O
Mammal	O
and	O
is	O
a	O
WingedAnimal	O
does	O
not	O
imply	O
that	O
it	O
is	O
an	O
Animal	O
twice	O
:	O
an	O
Animal	O
base	B-Application
class	I-Application
corresponds	O
to	O
a	O
contract	O
that	O
Bat	O
implements	O
(	O
the	O
"	O
is	O
a	O
"	O
relationship	O
above	O
really	O
means	O
"	O
implements	O
the	O
requirements	O
of	O
"	O
)	O
,	O
and	O
a	O
Bat	O
only	O
implements	O
the	O
Animal	O
contract	O
once	O
.	O
</s>
<s>
(	O
In	O
the	O
first	O
code	O
example	O
we	O
see	O
that	O
Eat	O
is	O
not	O
overridden	O
in	O
either	O
Mammal	O
or	O
WingedAnimal	O
,	O
so	O
the	O
two	O
Animal	O
subobjects	O
will	O
actually	O
behave	O
the	O
same	O
,	O
but	O
this	O
is	O
just	O
a	O
degenerate	O
case	O
,	O
and	O
that	O
does	O
not	O
make	O
a	O
difference	O
from	O
the	O
C++	B-Language
point	O
of	O
view	O
.	O
)	O
</s>
<s>
This	O
situation	O
is	O
sometimes	O
referred	O
to	O
as	O
diamond	O
inheritance	B-Language
(	O
see	O
Diamond	O
problem	O
)	O
because	O
the	O
inheritance	B-Language
diagram	O
is	O
in	O
the	O
shape	O
of	O
a	O
diamond	O
.	O
</s>
<s>
Virtual	B-Application
inheritance	I-Application
can	O
help	O
to	O
solve	O
this	O
problem	O
.	O
</s>
<s>
However	O
this	O
offset	O
can	O
in	O
the	O
general	O
case	O
only	O
be	O
known	O
at	O
runtime	O
,	O
thus	O
Bat	O
must	O
become	O
(	O
vpointer	B-Language
,	O
Mammal	O
,	O
vpointer	B-Language
,	O
WingedAnimal	O
,	O
Bat	O
,	O
Animal	O
)	O
.	O
</s>
<s>
There	O
are	O
two	O
vtable	B-Language
pointers	O
,	O
one	O
per	O
inheritance	B-Language
hierarchy	I-Language
that	O
virtually	O
inherits	O
Animal	O
.	O
</s>
<s>
All	O
objects	O
of	O
type	O
Bat	O
will	O
use	O
the	O
same	O
vpointers	B-Language
,	O
but	O
each	O
Bat	O
object	O
will	O
contain	O
its	O
own	O
unique	O
Animal	O
object	O
.	O
</s>
<s>
If	O
another	O
class	O
inherits	O
from	O
Mammal	O
,	O
such	O
as	O
Squirrel	O
,	O
then	O
the	O
vpointer	B-Language
in	O
the	O
Mammal	O
part	O
of	O
Squirrel	O
will	O
generally	O
be	O
different	O
to	O
the	O
vpointer	B-Language
in	O
the	O
Mammal	O
part	O
of	O
Bat	O
though	O
they	O
may	O
happen	O
to	O
be	O
the	O
same	O
if	O
the	O
Squirrel	O
class	O
be	O
the	O
same	O
size	O
as	O
Bat	O
.	O
</s>
<s>
This	O
example	O
to	O
illustrates	O
a	O
case	O
where	O
base	B-Application
class	I-Application
A	O
has	O
a	O
constructor	O
variable	O
msg	O
and	O
an	O
additional	O
ancestor	O
E	O
is	O
derived	O
from	O
grandchild	O
class	O
D	O
.	O
</s>
<s>
Here	O
,	O
A	O
must	O
be	O
constructed	O
in	O
both	O
D	O
and	O
E	O
.	O
Further	O
,	O
inspection	O
of	O
the	O
variable	O
msg	O
illustrates	O
how	O
class	O
A	O
becomes	O
a	O
direct	O
base	B-Application
class	I-Application
of	O
its	O
deriving	O
class	O
,	O
as	O
opposed	O
to	O
a	O
base	B-Application
class	I-Application
of	O
any	O
intermediate	O
deriving	O
classed	O
between	O
A	O
and	O
the	O
final	O
deriving	O
class	O
.	O
</s>
<s>
Suppose	O
a	O
pure	O
virtual	O
method	O
is	O
defined	O
in	O
the	O
base	B-Application
class	I-Application
.	O
</s>
<s>
If	O
a	O
deriving	O
class	O
inherits	O
the	O
base	B-Application
class	I-Application
virtually	O
,	O
then	O
the	O
pure	O
virtual	O
method	O
does	O
not	O
need	O
to	O
be	O
defined	O
in	O
that	O
deriving	O
class	O
.	O
</s>
<s>
However	O
,	O
if	O
the	O
deriving	O
class	O
does	O
not	O
inherit	O
the	O
base	B-Application
class	I-Application
virtually	O
,	O
then	O
all	O
virtual	O
methods	O
must	O
be	O
defined	O
.	O
</s>
