Data Members

A class can have static and nonstatic data members. The static storage class specifies a static data member; with no storage class, a data member is nonstatic. No other storage class specifier is allowed. Every object has its own copy of the class’s nonstatic data members, and they share a single copy of each static data member. A data member can also be declared with cv -qualifiers. See Chapter 2 for more information about storage class specifiers and cv-qualifiers.

You declare data members as you would local variables in a function, except you cannot usually supply initializers. Instead, nonstatic data members are initialized in the class’s constructors. See Section 6.3 later in this chapter for details.

Data members are typically declared at the private access level. See Section 6.5 later in this chapter for details.

Data Layout

image with no caption

Nonstatic data members are organized so that members declared later have higher addresses than those declared earlier. Access specifier labels, however, can interrupt the order, and the relative order of members separated by an access specifier label is implementation-defined. Writing code that depends on the layout of a class is usually a bad idea, but when interfacing with external code, it is sometimes unavoidable. See Section 6.1.1 earlier in this chapter for more information.

The layout of base-class subobjects within a derived-class object ...

Get C++ In a Nutshell 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.