The difference in size is due to vptr added by the compiler.
sizeof(Derived1) = 1 , this is because, according to C ++ standards, an empty class always takes 1 byte of memory.
sizeof(Derived2) = 8 , since it inherits the virtual base class Derived1, so vptr is added by the compiler ( sizeof(vptr) = 8 on the 64 bit machine), and therefore sizeof(Derived2) shows 8 bytes.
sizeof(Derived3) = 1 due to 1 char byte.
sizeof(Derived4) = 16 , the internal implementation of Virtual inheritance is completely dependent on the compiler, because of this you see 16 bytes as the size.
sizeof(Dummy) = 1 Because it contains a single char object.
source share