cpp/language/data members

Non-static data members are declared in a of a class.

Any are allowed, except
 * and storage class specifiers are not allowed;


 * ,, and arrays thereof are not allowed: in particular, a class cannot have a non-static data member of type , although it can have a non-static data member of type  (reference to C) or  (pointer to C);
 * a non-static data member cannot have the same name as the name of the class if at least one user-declared constructor is present;

In addition, declarations are allowed.

Layout
When an object of some class is created, each non-static data member of non-reference type is allocated in some part of the object representation of. Whether reference members occupy any storage is implementation-defined, but their is the same as that of the object in which they are members.

Alignment requirements may necessitate padding between members, or after the last member of a class.

Standard-layout
The common initial sequence of two standard-layout non-union class types is the longest sequence of non-static data members and bit-fields in declaration order, starting with the first such entity in each of the classes, such that


 * corresponding entities have layout-compatible types,
 * corresponding entities have the same, and
 * either both entities are bit-fields with the same width or neither is a bit-field.

Two standard-layout non-union class types are called layout-compatible if they are the same type ignoring cv-qualifiers, if any, are layout-compatible (i.e. enumerations with the same underlying type), or if their common initial sequence consists of every non-static data member and bit-field (in the example above,  and  are layout-compatible).

Two standard-layout unions are called layout-compatible if they have the same number of non-static data members and corresponding non-static data members (in any order) have layout-compatible types.

Standard-layout types have the following special properties:
 * In a standard-layout union with an active member of non-union class type, it is permitted to read a non-static data member of another union member of non-union class type  provided  is part of the common initial sequence of  and  (except that reading a volatile member through non-volatile glvalue is undefined).
 * A pointer to an object of standard-layout class type can be to pointer to its first non-static non-bitfield data member (if it has non-static data members) or otherwise any of its base class subobjects (if it has any), and vice versa. In other words, padding is not allowed before the first data member of a standard-layout type. Note that  rules still apply to the result of such cast.
 * The macro offsetof may be used to determine the offset of any member from the beginning of a standard-layout class.

Member initialization
Non-static data members may be initialized in one of two ways: @1@ In the of the constructor.

Usage
The name of a non-static data member or a non-static member function can only appear in the following three situations: @1@ As a part of class member access expression, in which the class either has this member or is derived from a class that has this member, including the implicit member access expressions that appear when a non-static member name is used in any of the contexts where  is allowed (inside member function bodies, in member initializer lists, in the in-class default member initializers).

@2@ To form a.

@3@ (for data members only, not member functions) When used in.

@@ Notes: such uses are allowed via the resolution of in, which is treated as a change in C++11 by some compilers (e.g. clang).