Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      Classes

      From cppreference.com
      <cpp‎ |language
       
       
      C++ language
      General topics
      Flow control
      Conditional execution statements
      Iteration statements (loops)
      Jump statements
      Functions
      Function declaration
      Lambda function expression
      inline specifier
      Dynamic exception specifications(until C++17*)
      noexcept specifier(C++11)
      Exceptions
      Namespaces
      Types
      Specifiers
      constexpr(C++11)
      consteval(C++20)
      constinit(C++20)
      Storage duration specifiers
      Initialization
      Expressions
      Alternative representations
      Literals
      Boolean -Integer -Floating-point
      Character -String -nullptr(C++11)
      User-defined(C++11)
      Utilities
      Attributes(C++11)
      Types
      typedef declaration
      Type alias declaration(C++11)
      Casts
      Memory allocation
      Classes
      Class-specific function properties
      Special member functions
      Templates
      Miscellaneous
       
       

      A class is a user-defined type.

      A class type is defined by class-specifier, which appears indecl-specifier-seq of thedeclaration syntax. Seeclass declaration for the syntax of the class specifier.

      A class can have the following kinds of members:

      1) data members:
      a)non-static data members, includingbit-fields.
      b)static data members
      2) member functions:
      a)non-static member functions
      b)static member functions
      3) nested types:
      a)nested classes andenumerations defined within the class definition
      b) aliases of existing types, defined withtypedefortype 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 oflookup (except when used to name aconstructor): this is known asinjected-class-name
      4)enumerators from all unscoped enumerations defined within the class, or introduced byusing-declarations orusing-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 classT cannot useT 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 nameT as long as there are no user-declared constructors.

      A class with at least one declared or inheritedvirtual member function ispolymorphic. Objects of this type arepolymorphic objects and have runtime type information stored as part of the object representation, which may be queried withdynamic_cast andtypeid. Virtual member functions participate in dynamic binding.

      A class with at least one declared or inherited pure virtual member function is anabstract class. Objects of this type cannot be created.

      A class with aconstexpr constructor is aLiteralType: objects of this type can be manipulated byconstexpr functions at compile time.

      (since C++11)

      Contents

      [edit]Properties of classes

      Trivially copyable class

      Atrivially copyable class is a class that

      Trivial class

      Atrivial class is a class that

      (deprecated in C++26)

      Standard-layout class

      Astandard-layout class is a class that

      • has nonon-static data members of type non-standard-layout class (or array of such types) or reference,
      • has novirtual functions and novirtual base classes,
      • has the sameaccess control for all non-static data members,
      • has no non-standard-layout base classes,
      • only one class in the hierarchy has non-static data members, and
      • Informally, none of the base classes has the same type as the first non-static data member. Or, formally: 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.

      Astandard-layout struct is a standard-layout class defined with the class keywordstruct or the class keywordclass. Astandard-layout union is a standard-layout class defined with the class keywordunion.

      (since C++11)

      [edit]Implicit-lifetime class

      Animplicit-lifetime class is a class that

      • is anaggregate whose destructor is notuser-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 reportP0593R6.

      [edit]POD class

      APOD class is a class that

      • is anaggregate,
      • 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)

      APOD struct is a non-union POD class. APOD 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.

      DRApplied toBehavior as publishedCorrect behavior
      CWG 148C++98POD classes could not contain pointers to member,
      which are themselves POD (scalar) types
      restriction removed
      CWG 383C++98copy assignment operators or destructors could be
      user-declared in POD classes if they are not defined
      not allowed
      CWG 1363C++11a class that has both trivial default constructors and non-trivial
      default constructors at the same time could be trivial
      it is non-trivial
      CWG 1496C++11a class that only has constructors that
      are all defined as deleted could be trivial
      it is non-trivial
      CWG 1672C++11a class could be a standard-layout class
      if it has multiple empty base classes
      it is not a standard-layout class
      CWG 1734C++11a trivially copyable class could not have non-trivial
      deleted copy/move constructors/assignment operators
      can be trivial if deleted
      CWG 1813C++11a class was never a standard-layout class if it has a
      base class that inherits a non-static data member
      it can be a standard-layout class
      CWG 1881C++11for a standard-layout class and its base classes,
      unnamed bit-fields might be declared in a
      different class declaring the data members
      all non-static data members
      and bit-fields need to be first
      declared in the same class
      CWG 1909C++98a member template could have the same name as its classprohibited
      CWG 2120C++11the definition of M(X) in determining a standard-
      layout class did not consider the case of
      a class whose first member is an array
      addressed this case in
      the definition of M(X)
      CWG 2605C++98an implicit-lifetime class could have a user-provided destructorprohibited
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/language/classes&oldid=178050"

      [8]ページ先頭

      ©2009-2025 Movatter.jp