Hey I'm trying to understand how size of class work when I'm using inheritance, so I wrote this code:
class AA
{
public :
int a;
virtual int getSize() {return sizeof(*this);}
};
class BB : public AA
{
public :
int b;
virtual int getSize2() {return sizeof(*this);}
};
int _tmain(int argc, _TCHAR* argv[])
{
AA aa;
BB bb;
std::cout << "Class AA : " << aa.getSize() << std::endl;
std::cout << "Class BB : " << bb.getSize() << std::endl;
std::cout << "Class BB : " << bb.getSize2() << std::endl;
}
The output of this code is :
Class AA : 8
Class BB : 8
Class BB : 12
My question is : why the second output return the size of AA instead than returning the size of BB? bb it's BB type, so I was expecting 12 instead of 8?