472,988 Members | 2,740 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,988 software developers and data experts.

Size of classed with virtual functions

I have a doubt with size of classed with virtual functions
I have declared A,A1,A2 ,B , C, D some classes with no varaibles but a
vitual function each,
The size of A is as expected 4 bytes with one vtbl ptr
BUt when I derive the class B from class A (both have 1 virtual function
each ) the size remains still as 4 bytes .
class A = 4 bytes
class B :public A = 4 bytes
class C:public A,public A1 size = 8 Bytes
class D :public A,public A1,public A2 size = 12 bytes

Can some body explain this anamlous behavior
Thanks
Vijay
Jul 19 '05 #1
4 4379

"vijay" <ge********@yahoo.com> wrote in message
news:bf**********@news.mch.sbs.de...
I suppose the
"John Harrison" <jo*************@hotmail.com> wrote in message
news:bf************@ID-196037.news.uni-berlin.de...

"vijay" <ge********@yahoo.com> wrote in message
news:bf**********@news.mch.sbs.de...
I have a doubt with size of classed with virtual functions
I have declared A,A1,A2 ,B , C, D some classes with no varaibles but a vitual function each,
The size of A is as expected 4 bytes with one vtbl ptr
BUt when I derive the class B from class A (both have 1 virtual function each ) the size remains still as 4 bytes .
class A = 4 bytes
class B :public A = 4 bytes
class C:public A,public A1 size = 8 Bytes
class D :public A,public A1,public A2 size = 12 bytes

Can some body explain this anamlous behavior
Thanks
Vijay

class A has one vtable with one virtual function (4 bytes)
class B also has one vtable with two functions (4 bytes), the virtual
functions from A and B are combined into a single vtable, in other words B inherits A's virtual function.
class C multiply inherits, so it has two vtables, first vtable has 2
functions (one from A and one from C), second vtable has one function

(from
A1), but two vtables is 8 bytes no matter how many functions.
class D multiply inherits, three vtables, 12 bytes

john
=====================
I suppose the vtable u r refering to is inderiectly the vtbl ptr present

in the first 4 bytes of the object layout


Yes.
One more point I wanted to add is that the virtual function present in each class is only the destructor of the respective classes !!!
So from John's explanation I can deduce that
If a derived class is multiply inherited ,then first base class virtual
functions will be inherited in the VTable if the class B
That means to say class B obect layout will have a Vtblr ptr in its first 4 bytes which will point to a vtable which has 2 entries
1entry is destructor of class B and
2 entry destructor of class A
But for class C
First Vtbl ptr in first 4 bytes of obj layout will strore Vtbl address which has class c, class A virtual functions
Second vtbl ptr in next 4 bytes of object lay out will store vtbl address
which has class A1 virtual functions

Similarly for class D
But I wish to know why this is done this way
Why cant there be 1 Vtbl ptr which stores Vtbl which has all three
classes. ie Class C, Class A, Class A1 virtual functions ???? since class C inherits from both the classes!!!!

Thanks
Vijay


Of course its worth mentioning that different compiler can choose different
methods, but I'd imagine that class D has three vtable pointers because of
code like this

D d;
A2* a2 = &d;

a2 now points to the A2 object that is contained in d. So there must be a
complete A2 object inside each D object, with its own vtable. Thats why D
must have three vtables, because it is an aggregate of three sub objects,
each of which must be complete in its own right.

There's a book called 'Inside the C++ Object Model' by Stanley Lippman that
explains all this sort of stuff.

john
Jul 19 '05 #2
[snip]

Hey vjay,

the implementation of inheritance is implementation dependent, but allocated
somewhere else. For the 'common' implementation you state, picture the
following:

1. Each class (not instance), but each class has a table which contains
points to virutal functions. For example:
virtual ~A() -> 0x00FFAABB
virtual int getValue ->0x00AABBDD
...
This table is not included in the sizeof() function. It is located
somewhere.

2. Each instance of a singly-inhertied class has a pointer to the table( in
1) for its class. This is used to resolve virtual function calls.

3. Each instance of multiple-inherited class has 'n' pointers to tables of
its 'n' class it derived from.

Suppose classes A, B, and C exists and contain virtual functions
an instance of each looks like the following:

[Instance of class A,B, or C]
*************
vTable pointer
instance data
*************

So, a multiple inherited class might look like this:
class D derives from A,B, and C

[instance of class D]
**********************
vTable pointer of class D if class D contains any virtual functions
Instance of A
Instance of B
Instance of C
Instance of D data
**********************

Now why don't they just make a whole new virtual table for class B, that
includes all the entries for class A, B, and C instead of storing the
v-table ptr for A as part of the A-instance? Well suppose we have the
following legal code:

D *d = new D();
A* a = d;

What exactly would 'a' be pointer to if a single v-table-ptr were used for
D? It is this neccessity that basically forces each instance to have its
own v-table ptr.

Yamin
Jul 19 '05 #3

"Yamin" <ab*****@sdfdasfsd.com> wrote in message
news:Sb********************@news02.bloor.is.net.ca ble.rogers.com...

"Yamin" <ab*****@sdfdasfsd.com> wrote in message
news:Q9********************@news02.bloor.is.net.ca ble.rogers.com...
[snip]

Hey vjay,

the implementation of inheritance is implementation dependent, but allocated
somewhere else. For the 'common' implementation you state, picture the
following:

1. Each class (not instance), but each class has a table which contains
points to virutal functions. For example:
virtual ~A() -> 0x00FFAABB
virtual int getValue ->0x00AABBDD
...
This table is not included in the sizeof() function. It is located
somewhere.

2. Each instance of a singly-inhertied class has a pointer to the

table( in
1) for its class. This is used to resolve virtual function calls.

3. Each instance of multiple-inherited class has 'n' pointers to tables

of
its 'n' class it derived from.

Suppose classes A, B, and C exists and contain virtual functions
an instance of each looks like the following:

[Instance of class A,B, or C]
*************
vTable pointer
instance data
*************

So, a multiple inherited class might look like this:
class D derives from A,B, and C

[instance of class D]
**********************
vTable pointer of class D if class D contains any virtual functions
Instance of A
Instance of B
Instance of C
Instance of D data
**********************

Now why don't they just make a whole new virtual table for class B, that


That should be class D
includes all the entries for class A, B, and C instead of storing the
v-table ptr for A as part of the A-instance? Well suppose we have the
following legal code:

D *d = new D();
A* a = d;

What exactly would 'a' be pointer to if a single v-table-ptr were used for D? It is this neccessity that basically forces each instance to have its own v-table ptr.

Yamin

Ya , I know this reason, But my idea asking this question is in different
frame,
Why should first deried inherited class as explained by John shud get
clubbed with base class
Again look at my question, why is the size of B is 4 bytes and not 8 bytes,,

well John
U r explanation is good, But well any way I wish to read it again and get
back to u if I am still at doubt
Vijay

Jul 19 '05 #4

"vijay" <ge********@yahoo.com> wrote in message
news:bf**********@news.mch.sbs.de...

"Yamin" <ab*****@sdfdasfsd.com> wrote in message
news:Sb********************@news02.bloor.is.net.ca ble.rogers.com...

"Yamin" <ab*****@sdfdasfsd.com> wrote in message
news:Q9********************@news02.bloor.is.net.ca ble.rogers.com...
[snip]

Hey vjay,

the implementation of inheritance is implementation dependent, but allocated
somewhere else. For the 'common' implementation you state, picture the following:

1. Each class (not instance), but each class has a table which contains points to virutal functions. For example:
virtual ~A() -> 0x00FFAABB
virtual int getValue ->0x00AABBDD
...
This table is not included in the sizeof() function. It is located
somewhere.

2. Each instance of a singly-inhertied class has a pointer to the

table( in
1) for its class. This is used to resolve virtual function calls.

3. Each instance of multiple-inherited class has 'n' pointers to
tables
of
its 'n' class it derived from.

Suppose classes A, B, and C exists and contain virtual functions
an instance of each looks like the following:

[Instance of class A,B, or C]
*************
vTable pointer
instance data
*************

So, a multiple inherited class might look like this:
class D derives from A,B, and C

[instance of class D]
**********************
vTable pointer of class D if class D contains any virtual functions
Instance of A
Instance of B
Instance of C
Instance of D data
**********************

Now why don't they just make a whole new virtual table for class B,
that
That should be class D
includes all the entries for class A, B, and C instead of storing the
v-table ptr for A as part of the A-instance? Well suppose we have the
following legal code:

D *d = new D();
A* a = d;

What exactly would 'a' be pointer to if a single v-table-ptr were used

for D? It is this neccessity that basically forces each instance to have its own v-table ptr.

Yamin

Ya , I know this reason, But my idea asking this question is in different
frame,
Why should first deried inherited class as explained by John shud get
clubbed with base class
Again look at my question, why is the size of B is 4 bytes and not 8

bytes,,
well John
U r explanation is good, But well any way I wish to read it again and get
back to u if I am still at doubt
Vijay


When you have a simple case like

class A { virtual void f(); };
class B : public A { virtual void f(); virtual void g(); };

Then A and B can safely share the same vtable. The vtable for B will have
B::f(), B::g() in that order. If you then write

B b;
A* a_ptr = &b;

then it doesn't matter that a_ptr has a vtable with entries for f and g,
because you can't call g using a_ptr, the compiler will prevent that.

a_ptr->f(); // OK, calls B::f()
a_ptr->g(); // illegal, no such function in A

john
Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

35
by: wired | last post by:
Hi, I've just taught myself C++, so I haven't learnt much about style or the like from any single source, and I'm quite styleless as a result. But at the same time, I really want nice code and I...
5
by: Ryan Faulkner | last post by:
Hi, Im having a few problems with virtual functions (Im using the Visual C++ environment by the way). I have a base class with three virtual functions and a derived class with a single new...
18
by: Tarundeep | last post by:
hi, let us say it is a 32 bit processor then the size of the pointer is 32 bits. now i want to know what would be the size of the class with vtable pointer in it , that is it has few virtual...
25
by: Stijn Oude Brunink | last post by:
Hello, I have the following trade off to make: A base class with 2 virtual functions would be realy helpfull for the problem I'm working on. Still though the functions that my program will use...
11
by: santosh | last post by:
Hello, I was going through the Marshal Cline's C++ FAQ-Lite. I have a doubt regarding section 33.10. Here he is declaring a pure virtual destructor in the base class. And again defining...
15
by: puzzlecracker | last post by:
Got Confused on the interview with memory alligment questions... PLEASE HELP -- How much bytes of memory will structs below take on 32 bit machine? What about 64 bit machine? Why is it different?...
8
by: François Fleuret | last post by:
Dear all, I fear this is a question which has been asked zillions of times. However, it seems to me to be such a legitimate feature that I can't imagine it is impossible. How can one know the...
14
by: v4vijayakumar | last post by:
Why we need "virtual private member functions"? Why it is not an (compile time) error?
11
by: Chris Thomasson | last post by:
Consider an an object that that can has 7 or 8 functions. If you create an abstract base class for the "interface" of the object, well, that means 7 or 8 pure virtual functions right? Well, IMHO,...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.