I am baffled by the VIRTUAL keyword. I am trying to find how does compiler implement this in memory. ok so let me explain with examples. I am using Microsoft Visual studio 2010 as implementation of virtual depends on compiler.
here is the first code
#include<iostream>
class one
{
int _a;
public:
virtual ~one(){}
};
class two:public one
{
int _a;
public:
virtual ~two(){}
};
int main()
{
using namespace std;
cout<<"sizeof two="<<sizeof(two)<<endl;
return 0;
}
o/p is 12 bytes, because of _vptr_two,one::_a and two::_a
here is another example code
#include<iostream>
class one
{
int _a;
public:
virtual ~one(){}
};
class two
{
int _a;
public:
virtual ~two(){}
};
class three:virtual public one,virtual public two
{
};
int main()
{
using namespace std;
cout<<"sizeof three="<<sizeof(three)<<endl;
return 0;
}
in this case o/p is 20 bytes :O, how come?? Please explain!! According to me it should be 16 bytes. __vptr_three(pointer to vtable), _vptr1_three(pointer to virtual base class table), one::_a and two::_a. And why virtual base class table is maintained ?