Say we have
Class A
{
public:
int _i;
virtual int getI();
};
class B : public A
{
public:
int _j;
virtual int getI();
};
So assuming that the size of a class in memory is the sum of its members (i.e. ignoring padding or whatever might actually happen), what is the size of a B instance? Is it sizeof(A) + sizeof(int) + sizeof(vptr)? Or does a B instance not hold an A vptr in it’s personal A instance, so that sizeof(b) would be sizeof(int) + sizeof(int) + sizeof(vptr)?
It’s whatever the implementation needs to make the code work. All you
can say is that it is at least
2 * sizeof(int), because objects oftype
Bcontain twoints (and possibly other things). In a typicalimplementation,
AandBwill share a vptr, and the total size willbe just one pointer more than the two ints (modulo padding for
alignment, but on most implementations, I don’t think that there will be
any). But that’s just a typical implementation; you can’t count on it.