A
asit
Please go through the following code...
#include <iostream>
using namespace std;
class Empty {};
class Derived1 : public Empty {};
class Derived2 : virtual public Empty {};
class Derived3 : public Empty
{
char c;
};
class Derived4 : virtual public Empty
{
char c;
};
class Dummy
{
char c;
};
int main()
{
cout<<"Sizeof(Empty) : "<<sizeof(Empty)<<endl;
cout<<"Sizeof(Derived1) : "<<sizeof(Derived1)<<endl;
cout<<"Sizeof(Derived2) : "<<sizeof(Derived2)<<endl;
cout<<"Sizeof(Derived3) : "<<sizeof(Derived3)<<endl;
cout<<"Sizeof(Derived4) : "<<sizeof(Derived4)<<endl;
cout<<"Sizeof(Dummy) : "<<sizeof(Dummy)<<endl;
return 0;
}
Why sizeof Derived4 is 8 bytes ?
#include <iostream>
using namespace std;
class Empty {};
class Derived1 : public Empty {};
class Derived2 : virtual public Empty {};
class Derived3 : public Empty
{
char c;
};
class Derived4 : virtual public Empty
{
char c;
};
class Dummy
{
char c;
};
int main()
{
cout<<"Sizeof(Empty) : "<<sizeof(Empty)<<endl;
cout<<"Sizeof(Derived1) : "<<sizeof(Derived1)<<endl;
cout<<"Sizeof(Derived2) : "<<sizeof(Derived2)<<endl;
cout<<"Sizeof(Derived3) : "<<sizeof(Derived3)<<endl;
cout<<"Sizeof(Derived4) : "<<sizeof(Derived4)<<endl;
cout<<"Sizeof(Dummy) : "<<sizeof(Dummy)<<endl;
return 0;
}
Why sizeof Derived4 is 8 bytes ?