Chapter 3. The Semantics of Data
Some while back I received e-mail from someone in France who was both mystified and a might upset. He had either volunteered or been drafted to provide a persistence library for his project group. In preparation for his work, he coded and then printed out the result of applying the sizeof operator to the following seemingly trivial class hierarchy:
class X {}; class Y : public virtual X {}; class Z : public virtual X {}; class A : public Y, public Z {};
None of these classes contains any explicit data—any anything, in fact, except an inheritance relationship—so he apparently believed the size of each class should be 0. It wasn’t, of course—not even the apparently benign class X:
sizeof X yielded 1 sizeof Y yielded ...
Get Inside the C++ Object Model now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.