cpp/language/object

C++ programs create, destroy, refer to, access, and manipulate objects.

An object, in C++, has
 * size (can be determined with );
 * alignment requirement (can be determined with );
 * (automatic, static, dynamic, thread-local);
 * (bounded by storage duration or temporary);
 * value (which may be indeterminate, e.g. for non-class types);
 * optionally, a.
 * optionally, a.

The following entities are not objects: value, reference, function, enumerator, type, non-static class member, template, class or function template specialization, namespace, parameter pack, and.

A variable is an object or a reference that is not a non-static data member, that is introduced by a.

Object creation
Objects can be explicitly created by s,, , changing the active member of a and evaluating expressions that require. The created object is uniquely defined in explicit object creation.

Objects of can also be implicitly created by
 * operations that begin lifetime of an array of type, in which case such objects are created in the array,
 * call to following allocating functions, in which case such objects are created in the allocated storage:
 * operator new
 * operator new[]
 * std
 * std
 * std


 * call to following object representation copying functions, in which case such objects are created in the destination region of storage or the result:
 * std
 * std

Zero or more objects may be created in the same region of storage, as long as doing so would give the program defined behavior. If such creation is impossible, e.g. due to conflicting operations, the behavior of the program is undefined. If multiple such sets of implicitly created objects would give the program defined behavior, it is unspecified which such set of objects is created. In other words, implicitly created objects are not required to be uniquely defined.

After implicitly creating objects within a specified region of storage, some operations produce a pointer to a suitable created object. The suitable created object has the same address as the region of storage. Likewise, the behavior is undefined if only if no such pointer value can give the program defined behavior, and it is unspecified which pointer value is produced if there are multiple values giving the program defined behavior.

Call to std or implicitly defined copy/move special member functions of types can also create objects.

Object representation and value representation
For an object of type :
 * its object representation is the sequence of objects of type   beginning at the same address as the  object,
 * its value representation is the set of bits that hold the value of its type, and
 * its padding bits are the bits in the object representation that are not part of the value representation.

For, value representation is a part of the object representation, which means that copying the bytes occupied by the object in the storage is sufficient to produce another object with the same value (except if the object is a potentially-overlapping subobject, or the value is a trap representation of its type and loading it into the CPU raises a hardware exception, such as SNaN ("signalling not-a-number") floating-point values or NaT ("not-a-thing") integers).

The reverse is not necessarily true: two objects of a with different object representations may represent the same value. For example, multiple floating-point bit patterns represent the same special value NaN. More commonly, padding bits may be introduced to satisfy, sizes, etc.

For the objects of type, , and (unless they are oversize ), every bit of the object representation is required to participate in the value representation and each possible bit pattern represents a distinct value (no padding bits, trap bits, or multiple representations allowed).

Subobjects
An object can have subobjects. These include An object that is not a subobject of another object is called complete object.
 * member objects
 * base class subobjects
 * array elements

A subobject is potentially overlapping if it is a base class subobject.

Complete objects, member objects, and array elements are also known as most derived objects, to distinguish them from base class subobjects. The size of an object that is neither potentially overlapping nor a is required to be non-zero (the size of a base class subobject may be zero : see ).

An object can contain other objects, in which case the contained objects are nested within the former object. An object is nested within another object  if
 * is a subobject of, or
 * for, or
 * there exists an object where  is nested within, and  is nested within.

Any two objects with overlapping s (that are not ) are guaranteed to have different addresses unless one of them is nested within another, or if they are subobjects of different type within the same complete object, and one of them is a subobject of zero size.

Polymorphic objects
Objects of a class type that declares or inherits at least one virtual function are polymorphic objects. Within each polymorphic object, the implementation stores additional information (in every existing implementation, it is one pointer unless optimized out), which is used by calls and by the RTTI features ( and ) to determine, at run time, the type with which the object was created, regardless of the expression it is used in.

For non-polymorphic objects, the interpretation of the value is determined from the expression in which the object is used, and is decided at compile time.

Strict aliasing
Accessing an object using an expression of a type other than the type with which it was created is undefined behavior in many cases, see for the list of exceptions and examples.

Alignment
Every has the property called alignment requirement, which is a nonnegative integer value (of type std, and always a power of two) representing the number of bytes between successive addresses at which objects of this type can be allocated.

Each object type imposes its alignment requirement on every object of that type.

In order to satisfy alignment requirements of all non-static members of a, padding bits may be inserted after some of its members.

The weakest alignment (the smallest alignment requirement) is the alignment of, , and , which equals ; the largest fundamental alignment of any type is implementation-defined.