Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      Abstract class

      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
       
       

      Defines an abstract type which cannot be instantiated, but can be used as a base class.

      Contents

      [edit]Syntax

      Apure virtual function is avirtual function whosedeclarator has the following syntax:

      declaratorvirt-specifier (optional)=0

      Here the sequence= 0 is known aspure-specifier, and appears either immediately after thedeclarator or after the optionalvirt-specifier (override orfinal).

      pure-specifier cannot appear in a member function definition orfriend declaration.

      struct Base{virtualint g();virtual ~Base(){}}; struct A: Base{// OK: declares three member virtual functions, two of them purevirtualint f()=0, g() override=0, h(); // OK: destructor can be pure too    ~A()=0; // Error: pure-specifier on a function definitionvirtualint b()=0{}};

      Anabstract class is a class that either defines or inherits at least one function for whichthe final overrider ispure virtual.

      [edit]Explanation

      Abstract classes are used to represent general concepts (for example, Shape, Animal), which can be used as base classes for concrete classes (for example, Circle, Dog).

      No objects of an abstract class can be created (except for base subobjects of a class derived from it) and no non-static data members whose type is an abstract class can be declared.

      Abstract types cannot be used as parameter types, as function return types, or as the type of an explicit conversion (note this is checked at the point of definition and function call, since at the point of function declaration parameter and return type may be incomplete).

      Pointers and references to an abstract class can be declared.

      Run this code
      struct Abstract{virtualvoid f()=0;// pure virtual};// "Abstract" is abstract struct Concrete: Abstract{void f() override{}// non-pure virtualvirtualvoid g();// non-pure virtual};// "Concrete" is non-abstract struct Abstract2: Concrete{void g() override=0;// pure virtual overrider};// "Abstract2" is abstract int main(){// Abstract a;   // Error: abstract class    Concrete b;// OK    Abstract& a= b;// OK to reference abstract base    a.f();// virtual dispatch to Concrete::f()// Abstract2 a2; // Error: abstract class (final overrider of g() is pure)}

      The definition of a pure virtual function may be provided (and must be provided if the pure virtual is thedestructor): the member functions of the derived class are free to call the abstract base's pure virtual function using qualified function id. This definition must be provided outside of the class body (the syntax of a function declaration doesn't allow both the pure specifier= 0 and a function body).

      Making a virtual call to a pure virtual function from a constructor or the destructor of the abstract class is undefined behavior (regardless of whether it has a definition or not).

      struct Abstract{virtualvoid f()=0;// pure virtualvirtualvoid g(){}// non-pure virtual     ~Abstract(){        g();// OK: calls Abstract::g()// f();        // undefined behavior        Abstract::f();// OK: non-virtual call}}; // definition of the pure virtual functionvoid Abstract::f(){std::cout<<"A::f()\n";} struct Concrete: Abstract{void f() override{        Abstract::f();// OK: calls pure virtual function} void g() override{}     ~Concrete(){        g();// OK: calls Concrete::g()        f();// OK: calls Concrete::f()}};

      [edit]Defect reports

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

      DRApplied toBehavior as publishedCorrect behavior
      CWG 390C++98an undefined pure virtual destructor might be calleda definition is required in this case
      CWG 2153C++98pure-specifier could appear in friend declarationsprohibited

      [edit]See also

      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/language/abstract_class&oldid=169685"

      [8]ページ先頭

      ©2009-2025 Movatter.jp