|
-
May 10th, 2010, 06:07 AM
#1
Virtual Function Pointer Table
Code:
#include <iostream>
using namespace std;
class Base1 {
public:
virtual void f() { }
};
class Base2 {
public:
virtual void f() { }
};
class Base3 {
public:
virtual void f() { }
};
class Drive : public Base1, public Base2, public Base3 {
};
int main() {
Drive objDrive;
cout << "Size is = " << sizeof(objDrive) << endl;
return 0;
}
Why is the sizeof(objDrive) being indicated as 12 ?
I understand that :
4 bytes for virtual fptr table for Base 1, 4 bytes for virtual fptr table for Base 2,4 bytes for virtual fptr table for Base 3.
What about 4 bytes for virtual fptr table for Drive ?
-
May 10th, 2010, 06:47 AM
#2
Re: Virtual Function Pointer Table
 Originally Posted by humble_learner
What about 4 bytes for virtual fptr table for Drive ?
Drive doesn't have virtual functions (except in its base classes).
Note that this is all very compiler dependent, so unless this is just for learning/curiosity, don't count on anything.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
-
May 11th, 2010, 12:26 AM
#3
Re: Virtual Function Pointer Table
Thanks, it is indeed for learning.
But I suppose, if Drive did indeed have a virtual function, then that would have reflected in the size right ?
-
May 11th, 2010, 01:19 PM
#4
Re: Virtual Function Pointer Table
First three classes are having virtual function, and thus they have separate vtables. When a class inherits from another class having vtables, it uses only that vtable.
For instance, if you derive from only one class - the derived class would be of 4 bytes only!
-
May 11th, 2010, 10:12 PM
#5
Re: Virtual Function Pointer Table
Also, never assume that a class or struct will have a size that's the sum of its components. 99 times out of 100 it'll work, but that 1% will teach you a good lesson.
-
May 11th, 2010, 10:21 PM
#6
Re: Virtual Function Pointer Table
 Originally Posted by karenrei
Also, never assume that a class or struct will have a size that's the sum of its components. 99 times out of 100 it'll work, but that 1% will teach you a good lesson.
From experience, that only works 50% of the time actually :/
When the object is anything more complex than a POD, I've yet to meet the person who could reliably predict object sizes.
Not that knowing the actual object size has any use though.
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
-
May 12th, 2010, 12:31 AM
#7
Re: Virtual Function Pointer Table
 Originally Posted by Ajay Vijay
First three classes are having virtual function, and thus they have separate vtables. When a class inherits from another class having vtables, it uses only that vtable.
For instance, if you derive from only one class - the derived class would be of 4 bytes only!
But what if I had derived (class C) from two classes A and B (each of which have a virtual function each), and then add another new virtual function in my class C.
Which of the virtual tables (A or B) would my class C use ?
Could you please explain what would happen in this case ?
-
May 12th, 2010, 09:53 AM
#8
Re: Virtual Function Pointer Table
Understand following points:
- The minimum size of a class is 1 byte - if it doesnt include any data members, doesnt inherit from a class and doesn't have any virtual function.
- Adding virtual function in a class requires it to use sizeof(pointer) bytes. Thus on 32 bit, it would need 4 bytes, and on 64-bit it would need 8 bytes. Thus, size of class now increases by 4/8 bytes. But, if class was empty (as described in point 1), it remains 4/8 bytes,
- When you inherit from one class, it also uses the same vtable - thus size of derived class doesn't increase.
- If you inherit from two classes, both having vtables, the size of new class is now +4/8 (and not +8/16) - since the size doesn't increase when you inherit from one class (point 3).
- Inherit from three classes, all having vtables, the size would increase by pointer-size * 2, and so on.
- If a class inherits from N vtable'd classes, and M non-vtabled classes, the sizeof derived class would be: (N-1)*sizeof(pointer) + sizeof all M classes.
I hope it clears everything!
-
May 12th, 2010, 09:55 AM
#9
Re: Virtual Function Pointer Table
As a side note, not all compilers implement virtual function the way I described. Though, it is the most common.
-
May 13th, 2010, 07:25 PM
#10
Re: Virtual Function Pointer Table
Are you asking about the virtual function table or the size of the class?
As stated before, the hidden VTable pointer only points to a data structure that holds the pointers to the virtual functions of that type, inherited, implemented, or new entries. So, the VTable pointer only takes up 4/8 bytes in the class object.
As far as the virtual function table, from my knowledge the rule is that they are placed in the table in the order they appear. So if you have:
Code:
class A
{
public:
virtual ~A() {}
virtual void Function1() {}
};
class B : public A
{
public:
virtual ~B() {}
virtual void Function1() {} // override
virtual void Function2() {}
};
// If you could imagine, then A's vtable would look something like this. Keep in mind
// you cannot have a pointer to a destructor or constructor, so I'll have to
// use an MS specific __thiscall global function to represent the destructors
// in the VTable.
struct A_VTable
{
void (__thiscall * Destructor)(A * pObject);
void (A::*Function1)();
};
// B inherits A, overrides the destructor and Function1 slots, so it's kind of like.
struct B_VTable
{
void (__thiscall * Destructor)(B * pObject);
void (B::*Function1)();
void (B::*Function2)();
};
// The vtable pointer would just point at a place in the executable that the compiler/linker generated that is just a data structure filled with function pointers.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|