Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      Derived 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
       
       

      Any class type (whether declared withclass-keyclass orstruct) may be declared asderived from one or morebase classes which, in turn, may be derived from their own base classes, forming an inheritance hierarchy.

      Contents

      [edit]Syntax

      The list of base classes is provided in thebase-clause of theclass declaration syntax. Thebase-clause consists of the character: followed by a comma-separated list of one or morebase-specifiers.

      attr (optional)class-or-computed (1)
      attr (optional)virtualclass-or-computed (2)
      attr (optional)access-specifierclass-or-computed (3)
      attr (optional)virtualaccess-specifierclass-or-computed (4)
      attr (optional)access-specifiervirtualclass-or-computed (5)
      1) Specifies a non-virtual inheritance with default member accessibility.
      2) Specifies a virtual inheritance with default member accessibility.
      3) Specifies a non-virtual inheritance with given member accessibility.
      4) Specifies a virtual inheritance with given member accessibility.
      5) Same as 4),virtual andaccess-specifier can appear in any order.
      attr -(since C++11) sequence of any number ofattributes
      access-specifier - one ofprivate,public, orprotected
      class-or-computed - one of
      • nested-name-specifier (optional)type-name
      • nested-name-specifiertemplatesimple-template-id
      (since C++11)
      (since C++26)

      Anelaborated type specifier cannot directly appear asclass-or-computed due to syntax limitations.

      base-specifiers in abase-clause may bepack expansions.

      A class or struct declaredfinal cannot be denoted byclass-or-computed.

      (since C++11)

      Ifaccess-specifier is omitted, it defaults topublic for derived classes declared withclass-keystruct and toprivate for derived classes declared withclass-keyclass.

      struct Base{int a, b, c;}; // every object of type Derived includes Base as a subobjectstruct Derived: Base{int b;}; // every object of type Derived2 includes Derived and Base as subobjectsstruct Derived2: Derived{int c;};

      Classes denoted byclass-or-computed's listed in thebase-clause are direct base classes. Their bases are indirect base classes. The same class cannot be specified as a direct base class more than once, but the same class can be both direct and indirect base class.

      Each direct and indirect base class is present, asbase class subobject, within the object representation of the derived class at an ABI-dependent offset. Empty base classes usually do not increase the size of the derived object due toempty base optimization. The constructors of base class subobjects are called by the constructor of the derived class: arguments may be provided to those constructors in themember initializer list.

      [edit]Virtual base classes

      For each distinct base class that is specifiedvirtual, themost derived object contains only one base class subobject of that type, even if the class appears many times in the inheritance hierarchy (as long as it is inheritedvirtual every time).

      struct B{int n;};class X:publicvirtual B{};class Y:virtualpublic B{};class Z:public B{}; // every object of type AA has one X, one Y, one Z, and two B's:// one that is the base of Z and one that is shared by X and Ystruct AA: X, Y, Z{    AA(){        X::n=1;// modifies the virtual B subobject's member        Y::n=2;// modifies the same virtual B subobject's member        Z::n=3;// modifies the non-virtual B subobject's member std::cout<< X::n<< Y::n<< Z::n<<'\n';// prints 223}};

      An example of an inheritance hierarchy with virtual base classes is the iostreams hierarchy of the standard library:std::istream andstd::ostream are derived fromstd::ios using virtual inheritance.std::iostream is derived from bothstd::istream andstd::ostream, so every instance ofstd::iostream contains astd::ostream subobject, astd::istream subobject, and just onestd::ios subobject (and, consequently, onestd::ios_base).

      All virtual base subobjects are initialized before any non-virtual base subobject, so only themost derived class calls the constructors of the virtual bases in itsmember initializer list:

      struct B{int n;     B(int x): n(x){}}; struct X:virtual B{ X(): B(1){}};struct Y:virtual B{ Y(): B(2){}};struct AA: X, Y{ AA(): B(3), X(), Y(){}}; // the default constructor of AA calls the default constructors of X and Y// but those constructors do not call the constructor of B because B is a virtual baseAA a;// a.n == 3 // the default constructor of X calls the constructor of BX x;// x.n == 1

      There arespecial rules for unqualified name lookup for class members when virtual inheritance is involved (sometimes referred to as the rules of dominance).

      [edit]Public inheritance

      When a class usespublicmember access specifier to derive from a base, all public members of the base class are accessible as public members of the derived class and all protected members of the base class are accessible as protected members of the derived class (private members of the base are never accessible unless friended).

      Public inheritance models the subtyping relationship of object-oriented programming: the derived class object IS-A base class object. References and pointers to a derived object are expected to be usable by any code that expects references or pointers to any of its public bases (seeLSP) or, inDbC terms, a derived class should maintain class invariants of its public bases, should not strengthen any precondition or weaken any postcondition of a member function itoverrides.

      Run this code
      #include <iostream>#include <string>#include <vector> struct MenuOption{std::string title;}; // Menu is a vector of MenuOption: options can be inserted, removed, reordered...// and has a title.class Menu:publicstd::vector<MenuOption>{public:std::string title; void print()const{std::cout<< title<<":\n";for(std::size_t i=0, s= size(); i< s;++i)std::cout<<"  "<<(i+1)<<". "<< at(i).title<<'\n';}};// Note: Menu::title is not problematic because its role is independent of the base class. enumclass Color{ WHITE, RED, BLUE, GREEN}; void apply_terminal_color(Color){/* OS-specific */} // THIS IS BAD!// ColorMenu is a Menu where every option has a custom color.class ColorMenu:public Menu{public:std::vector<Color> colors; void print()const{std::cout<< title<<":\n"; for(std::size_t i=0, s= size(); i< s;++i){std::cout<<"  "<<(i+1)<<". ";            apply_terminal_color(colors[i]);std::cout<< at(i).title<<'\n';            apply_terminal_color(Color::WHITE);}}};// ColorMenu needs the following invariants that cannot be satisfied// by publicly inheriting from Menu, for example:// - ColorMenu::colors and Menu must have the same number of elements// - To make sense, calling erase() should remove also elements from colors,//   in order to let options keep their colors// Basically every non-const call to a std::vector method will break the invariant// of the ColorMenu and will need fixing from the user by correctly managing colors. int main(){    ColorMenu color_menu; // The big problem of this class is that we must keep ColorMenu::Color// in sync with Menu.    color_menu.push_back(MenuOption{"Some choice"}); // color_menu.print(); // ERROR! colors[i] in print() is out of range     color_menu.colors.push_back(Color::RED);     color_menu.print();// OK: colors and Menu has the same number of elements}

      [edit]Protected inheritance

      When a class usesprotectedmember access specifier to derive from a base, all public and protected members of the base class are accessible as protected members of the derived class (private members of the base are never accessible unless friended).

      Protected inheritance may be used for "controlled polymorphism": within the members of Derived, as well as within the members of all further-derived classes, the derived class IS-A base: references and pointers to Derived may be used where references and pointers to Base are expected.

      [edit]Private inheritance

      When a class usesprivatemember access specifier to derive from a base, all public and protected members of the base class are accessible as private members of the derived class (private members of the base are never accessible unless friended).

      Private inheritance is commonly used in policy-based design, since policies are usually empty classes, and using them as bases both enables static polymorphism and leveragesempty-base optimization.

      Private inheritance can also be used to implement the composition relationship (the base class subobject is an implementation detail of the derived class object). Using a member offers better encapsulation and is generally preferred unless the derived class requires access to protected members (including constructors) of the base, needs to override a virtual member of the base, needs the base to be constructed before and destructed after some other base subobject, needs to share a virtual base or needs to control the construction of a virtual base. Use of members to implement composition is also not applicable in the case of multiple inheritance from aparameter pack or when the identities of the base classes are determined at compile time through template metaprogramming.

      Similar to protected inheritance, private inheritance may also be used for controlled polymorphism: within the members of the derived (but not within further-derived classes), derived IS-A base.

      template<typename Transport>class service:private Transport// private inheritance from the Transport policy{public:void transmit(){        this->send(...);// send using whatever transport was supplied}}; // TCP transport policyclass tcp{public:void send(...);}; // UDP transport policyclass udp{public:void send(...);}; service<tcp> service(host, port); service.transmit(...);// send over TCP

      [edit]Member name lookup

      Unqualified and qualified name lookup rules for class members are detailed inname lookup.

      [edit]Keywords

      virtual

      [edit]Defect reports

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

      DRApplied toBehavior as publishedCorrect behavior
      CWG 1710C++98the syntax ofclass-or-decltype made it impossible to derive from
      a dependent class where thetemplate disambiguator is required
      allowedtemplate

      [edit]See also

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

      [8]ページ先頭

      ©2009-2025 Movatter.jp