Discussion:
Vitrual function table layouts
(too old to reply)
JY
2009-09-07 14:16:01 UTC
Permalink
Hi,

Given the following classes:

class A
{
virutual void a();
};

class B
{
virtual void b();
};

class C{}: public A, public B
{
vitual voic c();
};

1. How many vptrs and vtables would an instance of Class C have?

2. If I make the inheritance virtual, i.e.

class C{}: public virtual A, public virtual B
{
vitual voic c();
};

would it make any difference?

TIA,
JY
Igor Tandetnik
2009-09-07 14:30:48 UTC
Permalink
Post by JY
class A
{
virutual void a();
};
class B
{
virtual void b();
};
class C{}: public A, public B
{
vitual voic c();
};
1. How many vptrs and vtables would an instance of Class C have?
I expect two. It should be easy to infer from a test program that prints
sizeof(C). Printing offsets of A and B within C may also prove
illuminating:

C c;
printf("%d %d\n",
(char*)(A*)&c - (char*)&c,
(char*)(B*)&c - (char*)&c);
Post by JY
2. If I make the inheritance virtual, i.e.
class C{}: public virtual A, public virtual B
{
vitual voic c();
};
would it make any difference?
Probably. I definitely expect sizeof(C) to become larger, to accomodate
additional information needed to locate its A and B subobjects (which
may no longer be inside the C object itself). Again, a sample program
inspecting the layout of the class may better answer your questions.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925
Scot T Brennecke
2009-09-07 18:54:35 UTC
Permalink
Post by JY
Hi,
class A
{
virutual void a();
};
class B
{
virtual void b();
};
class C{}: public A, public B
{
vitual voic c();
};
1. How many vptrs and vtables would an instance of Class C have?
2. If I make the inheritance virtual, i.e.
class C{}: public virtual A, public virtual B
{
vitual voic c();
};
would it make any difference?
TIA,
JY
This looks suspiciously like a homework assignment in a C++ course.
Carl Daniel [VC++ MVP]
2009-09-08 04:09:42 UTC
Permalink
Post by JY
1. How many vptrs and vtables would an instance of Class C have?
2. If I make the inheritance virtual, i.e.
class C{}: public virtual A, public virtual B
{
vitual voic c();
};
would it make any difference?
http://www.openrce.org/articles/files/jangrayhood.pdf

Will tell you everything you need to know, and then some. This stuff hasn't
changed ... well, ever (as far as 32 bit VC++ is concerned anyway).

-cd

Loading...