O'Reilly logo

Inside the C++ Object Model by Stanley B. Lippman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required