Code:
#include "stdafx.h"
#include <iostream>
using namespace std;
class sample
{
int i;
virtual void fun(){};
};
class Derived_s: public virtual sample
{
};
class Derived_s2: public virtual sample
{
};
class Derived_m: public Derived_s, Derived_s2
{
};
int _tmain(int argc, _TCHAR* argv[])
{
Derived_s ds;
Derived_s2 ds2;
Derived_m dm;
cout<<sizeof(ds)<<" byte\n";
cout<<sizeof(ds2)<<" byte\n";
cout<<sizeof(dm)<<" byte\n"; //20 bye when no virtual decalred in deriving ds
//or else 16 byte
return 0;
}
result:
12 byte fine. 4(int)+ 4 (vptr)+ 4 vptr for inheritence ....fine
12 byte . same as above
16 byte . 4 (int) + 4 (vptr for function) + 2 x 4 (for each virtual inheritance vtpr)
Pls, Correct me if wrong.
Then , if remove virtual keyword from any of the derivation of Derived_s or Derived_s2 and make it like
Code:
class Derived_s: public sample
{
};
class Derived_s2: public virtual sample
{
};
the size of dm becomes 20. why?
or, also changing the above code like this,
class Derived_m: public virtual Derived_s, virtual Derived_s2
{
};
makes the size of dm as 20 bytes. why?
Pls, help me understand the issue claerly.
Warm Reagrds,
Alex