Click here to Skip to main content
15,880,543 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hello Guys,

Please answer below query.

Question: a class A has one virtual function, class B has one virtual function, class C has one virtual function.

Class D derives class A, B , C as private, protected, public respectively.

Then what would be size of class D?

Code:
C#
Class A
{
   public:
   virtual void fun1();
}

Class B
{  
   public:
   virtual void fun2();
}

Class C
{
   public:
   virtual void fun3();
}



Class D : private A, protected B, public C
{
   // There is not any virtual function of D class as A, B, C
}



1- What will be size of class D in bytes.
2- If in class B virtual keyword is removed and all other things are same then what would be size of class D?


Thanks in advance.


Regards,
Joy
Posted
Updated 8-Apr-14 22:45pm
v2
Comments
phil.o 9-Apr-14 4:59am    
Multiple inheritance is not allowed in .NET. Making your question a little weird.
johannesnestler 9-Apr-14 5:50am    
Wrong tagged question, belongs to C++ only
ZurdoDev 9-Apr-14 7:28am    
Homework?
Philippe Mori 9-Apr-14 14:31pm    
Class size is not defined by any standard and is implementation dependant.

It depends.

I think the size of the v-table is an implementation detail of the compiler rather than of the specification (I might be wrong, though) and that means that the size might differ across compilers.

That said, most are probably implemented in a very similar manner, so in your example I'd expect the size of D to be 12 if B::fun2 is virtual and 8 otherwise.

And that's on a 32 bit build, the figures would double on a 64 bit.

Hope this helps,
Fredrik
 
Share this answer
 
Comments
joyjhonson 10-Apr-14 6:10am    
Well, your explanation is helpfull.
See below class.

class A

{
public:

int a;
int b;
int c;

void fun1(int a, int b);
void fun2(int a, int b);

};

I created object of above class and used sizeof to get the sizeof the object of class.

I got answer as 12 bytes. I am on to 32-bit.

I am observing that, function size is not contributed in class size? can u please explain why?
Fredrik Bornander 10-Apr-14 6:13am    
The function is not "inside" the class, it's somewhere else in memory. The class merely holds a pointer (4 bytes for 32 bits) that is the address of the function.
Existence of virtual function(s) will add 4 bytes of virtual table pointer in the class, which will be added to size of class. Again, in this case, if the base class of the class already has virtual function(s) either directly or through its base class, then this additional virtual function won't add anything to the size of the class. Virtual table pointer will be common across the class hierarchy.

1-

_vptr of A class = 4 bytes
_vptr of B class = 4 bytes
_vptr of C class = 4 bytes

So Class D will be 12 bytes.

2 -

_vptr of A class = 4 bytes
B class = 1 byte, because it's not allowed for a class to be of size 0. The functions have nothing to do with it. It's just a dummy byte
_vptr of C class = 4 bytes

So Class D will be 9 bytes.
 
Share this answer
 
Comments
Philippe Mori 9-Apr-14 14:29pm    
If the compiler do empty class optimization, it will be 8 bytes if B does not have any virtual function. Also generally the memory is aligned so if that optimization is not done, size would probably be 12 bytes anyway.

In any case, the size is implementation dépendent (compiler, options, Platform...)
joyjhonson 10-Apr-14 5:46am    
Thanks your answer was usefull.
joyjhonson 10-Apr-14 5:51am    
So, I understood that, any if a class has virtual function( Either directly or through it's base), a virtual table pointer of 4 bytes ( in 32 bit OS) will be added.

If I understood correctly, I have one question here.

a) Suppose in class A, there were two virtual functions then what would be size? does number of virtual function matters?

b) how about size of a class which does not have any virtual function directly or through its base?
Emre Ataseven 10-Apr-14 7:04am    
Only the first virtual function in a class increases its size (compiler-dependent, but on most - if not all - it's like this). All subsequent methods do not. Non-virtual functions do not affect the class's size. This happens because a class instance doesn't hold pointers to methods themselves, but to a virtual function table, which is one per class.
joyjhonson 11-Apr-14 1:48am    
Nice explanations!! Thanks alot

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900