By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,116 Members | 1,319 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,116 IT Pros & Developers. It's quick & easy.

vtable creation

P: 2
Suppose class D is derived from class B. Assume there are some virtual fns in B and are overriden in D.

Code: B *b = new D;
1. What are the sequence of steps of the compiler in creating the vtable for class D and B?


Thanks
Sameer G
Aug 10 '07 #1
Share this Question
Share on Google+
7 Replies


Expert 10K+
P: 11,448
Suppose class D is derived from class B. Assume there are some virtual fns in B and are overriden in D.

Code: B *b = new D;
1. What are the sequence of steps of the compiler in creating the vtable for class D and B?


Thanks
Sameer G
It uses a copy of B's vtable and overwrites the entries for the virtual functions that
are overridden by D.

kind regards,

Jos
Aug 10 '07 #2

weaknessforcats
Expert Mod 5K+
P: 9,197
It uses a copy of B's vtable and overwrites the entries for the virtual functions that
are overridden by D.
Actually, it creates a new vtable from B's vtable plus any new virtual functions declared in D. Then it overwrites the entries from B that are overridden in D.
Aug 10 '07 #3

Expert 10K+
P: 11,448
Actually, it creates a new vtable from B's vtable plus any new virtual functions declared in D. Then it overwrites the entries from B that are overridden in D.
But that's what I wrote "it uses a copy of B's vtable"; of couse D's vtable could
need more entries in case of D's own virtual functions.

kind regards,

Jos
Aug 10 '07 #4

weaknessforcats
Expert Mod 5K+
P: 9,197
It wasn't clear that D's vtable could be larger though I'm sure that's what you meant.
Aug 10 '07 #5

Expert 10K+
P: 11,448
It wasn't clear that D's vtable could be larger though I'm sure that's what you meant.
Ah, well, maybe I assumed a few things to be known too much ;-) Sorry, for not
expressing myself clearly.

kind regards,

Jos
Aug 10 '07 #6

P: 2
Thanks you all for your replies.

a. Where is the VTABLE created by compiler (global / stack?)
b. Size of class OR object doesnot include the size of the vtable but only vtble pointer (TRUE?)

Thanks
Sam
Aug 13 '07 #7

weaknessforcats
Expert Mod 5K+
P: 9,197
The vtable is part of the code segment so is not on either the stack or the heap.

The sizeof a class with no data members and no virtual functions is 1 byte.

The sizeof a class with no data members and one or more virtual functions is 4 bytes, which is the vtable pointer inside the object.

Therefore, the sizeof an object includes only the vtable pointer.

There is one vtable for each class and not one for each object.
Aug 14 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.