c++ - Size of a empty Class & Derived Virtual Class -


1.why size of derived4 class shows 8 bytes ??

class empty {}; class derived4 : virtual public empty {     char c; }; 

2.while size of derived2 class shows 4 byte ??

class empty {}; class derived2 : virtual public empty {}; 

note sizeof(any_class) implementation-defined.

but happens in case. well, using virtual inheritance, implementations use hidden pointer implement feature cost sizeof(pointer) bytes (the pointer stored in derived class itself), plus sizeof members (if any), plus padding if necessary, plus sizeof base class(es) (which reduced zero, in case of empty base class, due empty-base-optimization).

for more detailed answer, search "padding in c++" on site. find lots of topics on it.


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -