Thursday, November 18, 2010

Empty Base Class Optimization (EBCO)


   class Base
   { }; 

   class Derived : public Base 
   { }; 
   class DerivedBaseDerived : public Base, public Derived
   { };

If your compiler implements the Empty Base Class Optimization (EBCO) and the sizeof(Base) == 1, then the sizeof(DerivedBaseDerived) is 2.

Sinces there are two subobjects, they need to have different addresses. So, one byte will be used for one subobject, and the other will be used for the other subobject and the object.

Reference: C++ Templates: The complete guide by David Vandevoorde and Nicolai M. Josuttis. Addison-Wesley, 2003, pp. 289-292.

No comments:

Post a Comment