Size of deriverd class with overriding virtual functions from base class

Hi,
The compiler creates virtual table for the base class and also for the derived class whether we override it or not.

That means each class has separate virtual table. when we get the size of the each class with out any data members...
the size of base is -- 4 bytes(64 bit)
and the size of derived is -- 1

The size of base class 4 is correct since it creates the virtual pointer internally and its size is member data + virtual pointer, but it in this case I have included any data members so it has given 4 byts.

But why in case of derived is 1 byte, since it the derived class has overridden the virtual function from base, this will also contains the virtual pointer which will be pointing to derived class Vtable, it the size of the class suppose to be 4 instead of 1 byte.

#include<iostream>

class A{
public:

virtual void display()
{
}
};

class B{
public:
void display()
{
}
};


int _tmain(int argc, _TCHAR* argv[])
{
A a;
std::cout<<sizeof(a)<<std::endl;
B b;
std::cout<<sizeof(b);
getchar();
return 0;
}

why derived class size is 1( it is suppose to be 4)?
Last edited on
B does not extend A in your code...
Topic archived. No new replies allowed.