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 4 4411
"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
[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
"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
"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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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...
|
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...
|
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...
|
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...
| |
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?...
|
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...
|
by: v4vijayakumar |
last post by:
Why we need "virtual private member functions"? Why it is not an
(compile time) error?
|
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,...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |