object (C++)
From Cppwiki
In C++, an object is an instance of a type (which may or may not be a class type, unlike in object-oriented programming). It occupies a properly-aligned region of storage consisting of one or more bytes. Note that functions are not considered objects in C++.
Generally speaking, each object occupies a distinct region of storage (that is, they do not "overlap" in memory). However, an object may share its storage with subobjects, such as the elements in an array or the members of a class type. A derived class object also contains a subobject for each of its base classes. Finally, all members of a union object share a common region of storage.
Lifetime
The minimum potential lifetime of an object is specified by its storage duration (3.7). An object exists after a region of storage has been allocated, and its constructor (if present) has been called. An object must be initialized before its value can be used, or else undefined behavior will occur. It ceases to exist after its destructor (if present) has been called, and its storage has been deallocated. (Note: Once the object is destroyed or deallocated, do not use its value.)
A const object must be initialized upon creation, after which its value cannot change.
Addressing
Every object has a unique address that is not directly convertible to the address of another type in a portable manner, except for the following:
- The address of an object is convertible to the address of the first byte in its region of storage, and vice versa.
- The address of an POD or aggregate object may be converted to the address of its first subobject, and vice versa. Additionally, the following hold:
- The
offsetofmacro in<cstddef>(<stddef.h>in C) can be used in conjunction with pointer arithmetic on pointers-to-charto convert between a pointer to an POD object and a pointer to one of its subobjects, as well as to measure the distance (in bytes) between the two. - The address of a member of a union may be converted to the address of any other member. (Each of these may also be considered the "first subobject" for this purpose.)
- The addresses of elements in an array may be mutually converted by adding/subtracting the difference in their indices (e.g. the address of
a[2]can be converted to the address ofa[5]by adding 3 to it).
- The
- The address of a derived class object may be converted to the address of any of its base class subobjects, and vice versa (except for virtual base classes).
References as objects
A reference, once initialized, is indistinguishable from its referent, or the object to which it refers. In this respect, one might say that a reference is not object in its own right.