cpp / latest / language / classes.html /

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:

1) data members:
2) member functions:
3) nested types:
a) nested classes and enumerations defined within the class definition
b) aliases of existing types, defined with typedef or type alias (since C++11)declarations
c) the name of the class within its own definition acts as a public member type alias of itself for the purpose of lookup (except when used to name a constructor): this is known as injected-class-name
4) enumerators from all unscoped enumerations defined within the class , or introduced by using-declarations or using-enum-declarations (since C++20)
5) member templates ( variable templates, (since C++14)class templates or function templates) may appear in the body of any non-local class/struct/union.

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 unscoped enumeration, 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)

Properties of classes

Trivially copyable class

A trivially copyable class is a class that.

(until C++17)
  • has at least one non-deleted copy constructor, move constructor, copy assignment operator, or move assignment operator
  • each copy constructor is either trivial or deleted
  • each move assignment operator is either trivial or deleted
  • each copy constructor is either trivial or deleted
  • each move assignment operator is either trivial or deleted, and
  • has a non-deleted trivial destructor.
(since C++17)
(until C++20)
(since C++20)

Trivial class

A trivial class is a class that.

  • is trivially copyable, and
  • has one or more default constructors, all of which are either trivial or deleted and at least one of which is not deleted.
(until C++20)
(since C++20)

Standard-layout class

A standard-layout class is a class that.

  • either has no non-static data members in the most derived class and at most one base class with non-static data members, or has no base classes with non-static data members, and
  • has no base classes of the same type as the first non-static data member.
(until C++17)
  • has all non-static data members and bit-fields in the class and its base classes first declared in the same class, and
  • given the class as S, has no element of the set M(S) of types as a base class, where M(X) for a type X is defined as:
    • If X is a non-union class type with no (possibly inherited) non-static data members, the set M(X) is empty.
    • If X is a non-union class type whose first non-static data member has type X0 (where said member may be an anonymous union), the set M(X) consists of X0 and the elements of M(X0).
    • If X is a union type, the set M(X) is the union of all M(Ui) and the set containing all Ui, where each Ui is the type of the ith non-static data member of X.
    • If X is an array type with element type Xe, the set M(X) consists of Xe and the elements of M(Xe).
    • If X is a non-class, non-array type, the set M(X) is empty.
(since C++17)


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)

Implicit-lifetime class

An implicit-lifetime class is a class that.

  • is an aggregate, or
  • has at least one trivial eligible constructor and a trivial, non-deleted destructor.
(since C++20)

POD class

A POD class is a class that.

  • is an aggregate,
  • has no user-declared copy assignment operator,
  • has no user-declared destructor, and
  • has no non-static data members of type non-POD class (or array of such types) or reference.
(until C++11)
  • is a trivial class,
  • is a standard-layout class, and
  • has no non-static data members of type non-POD class (or array of such types).
(since C++11)


A POD struct is a non-union POD class. A POD union is a union that is a POD class.

(until C++20)

Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DR Applied to Behavior as published Correct behavior
CWG 148 C++98 POD classes could not contain pointers to member,
which are themselves POD (scalar) types
restriction removed
CWG 383 C++98 copy assignment operators or destructors could be
user-declared in POD classes if they are not defined
not allowed
CWG 1363 C++11 a class that has both trivial default constructors and non-trivial
default constructors at the same time could be trivial
it is non-trivial
CWG 1496 C++11 a class that only has constructors that
are all defined as deleted could be trivial
it is non-trivial

© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/language/classes