Classes
A class is a user-defined type.
A class type is defined by class-specifier, which appears in decl-specifier-seq of the declaration syntax. See class declaration for the syntax of the class specifier.
A class can have the following kinds of members:
All members are defined at once in the class definition, they cannot be added to an already-defined class (unlike the members of namespaces)
A member of a class T
cannot use T
as its name if the member is
- a static data member,
- a member function,
- a member type,
- a member template,
- an enumerator of an enumeration (unless the enumeration is scoped)(since C++11), or
- a member of a member anonymous union.
However, a non-static data member may use the name T
as long as there are no user-declared constructors.
A class with at least one declared or inherited virtual member function is polymorphic. Objects of this type are polymorphic objects and have runtime type information stored as part of the object representation, which may be queried with dynamic_cast
and typeid
. Virtual member functions participate in dynamic binding.
A class with at least one declared or inherited pure virtual member function is an abstract class. Objects of this type cannot be created.
A class with a constexpr constructor is a LiteralType: objects of this type can be manipulated by constexpr functions at compile time. |
(since C++11) |
Contents |
[edit] Properties of classes
Trivially copyable classA trivially copyable class is a class that
Standard-layout classA standard-layout class is a class that
A standard-layout struct is a standard-layout class defined with the class keyword struct or the class keyword class. A standard-layout union is a standard-layout class defined with the class keyword union. |
(since C++11) |
[edit] Implicit-lifetime class
An implicit-lifetime class is a class that
- is an aggregate whose destructor is not user-declared(until C++11)user-provided(since C++11), or
- has at least one trivial eligible constructor and a trivial, non-deleted destructor.
Notes: the implicit-lifetime property is clarified by defect report P0593R6.
[edit] POD classA POD class is a class that
A POD struct is a non-union POD class. A POD union is a union that is a POD class. |
(deprecated in C++20) |
[edit] Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|