# public cplusplus /draft

### Subversion checkout URL

You can clone with HTTPS or Subversion.

Fetching contributors…

Cannot retrieve contributors at this time

file 1022 lines (914 sloc) 33.778 kb
 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 \rSec0[class.derived]{Derived classes}%\indextext{derived~class|(}%gram: \rSec1[gram.derived]{Derived classes}%gram:\indextext{base~class~virtual|see{virtual~base~class}}\indextext{function, virtual|see{virtual~function}}\indextext{dynamic binding|see{virtual~function}}\pnum\indextext{base~class}%\indextext{inheritance}%\indextext{multiple~inheritance}%A list of base classes can be specified in a class definition usingthe notation:\begin{bnf}\nontermdef{base-clause}\br    \terminal{:} base-specifier-list\end{bnf}\begin{bnf}\nontermdef{base-specifier-list}\br    base-specifier \terminal{...}\opt\br    base-specifier-list \terminal{,} base-specifier \terminal{...}\opt\end{bnf}\begin{bnf}\nontermdef{base-specifier}\br    attribute-specifier-seq\opt base-type-specifier\br    attribute-specifier-seq\opt \terminal{virtual} access-specifier\opt base-type-specifier\br    attribute-specifier-seq\opt access-specifier \terminal{virtual}\opt base-type-specifier\end{bnf}\begin{bnf}\nontermdef{class-or-decltype}\br    nested-name-specifier\opt class-name\br    decltype-specifier\end{bnf}\begin{bnf}\nontermdef{base-type-specifier}\br    class-or-decltype\end{bnf}\indextext{specifier~access|see{access~specifier}}%%\begin{bnf}\nontermdef{access-specifier}\br    \terminal{private}\br    \terminal{protected}\br    \terminal{public}\end{bnf}The optional \grammarterm{attribute-specifier-seq} appertains to the \grammarterm{base-specifier}.\pnum\indextext{type!incomplete}%The type denoted by a \grammarterm{base-type-specifier} shall bea class type that is notanincompletely defined class (Clause~\ref{class}); this class is called a\indextext{base~class!direct}%\term{direct base class}for the class being defined.\indextext{base~class}%\indextext{derivation|see{inheritance}}%During the lookup for a base class name, non-type names areignored~(\ref{basic.scope.hiding}). If the name found is not a\grammarterm{class-name}, the program is ill-formed. A class \tcode{B} is abase class of a class \tcode{D} if it is a direct base class of\tcode{D} or a direct base class of one of \tcode{D}'s base classes.\indextext{base~class!indirect}%A class is an \term{indirect} base class of another if it is a baseclass but not a direct base class. A class is said to be (directly orindirectly) \term{derived} from its (direct or indirect) baseclasses.\enternoteSee Clause~\ref{class.access} for the meaning of\grammarterm{access-specifier}.\exitnote\indextext{access control!base~class member}%Unless redeclared in the derived class, members of a base class are alsoconsidered to be members of the derived class. The base class membersare said to be\indextext{inheritance}%\term{inherited}by the derived class. Inherited members can be referred to inexpressions in the same manner as other members of the derived class,unless their names are hidden or ambiguous~(\ref{class.member.lookup}).\indextext{operator!scope~resolution}%\enternoteThe scope resolution operator \tcode{::}~(\ref{expr.prim}) can be usedto refer to a direct or indirect base member explicitly. This allowsaccess to a name that has been redeclared in the derived class. Aderived class can itself serve as a base class subject to accesscontrol; see~\ref{class.access.base}. A pointer to a derived class can beimplicitly converted to a pointer to an accessible unambiguous baseclass~(\ref{conv.ptr}). An lvalue of a derived class type can be boundto a reference to an accessible unambiguous baseclass~(\ref{dcl.init.ref}).\exitnote\pnumThe \grammarterm{base-specifier-list} specifies the type of the\term{base class subobjects} contained in anobject of the derived class type.\enterexample\indextext{example!derived~class}%\begin{codeblock}struct Base {  int a, b, c;};\end{codeblock}\begin{codeblock}struct Derived : Base {  int b;};\end{codeblock}\begin{codeblock}struct Derived2 : Derived {  int c;};\end{codeblock}Here, an object of class \tcode{Derived2} will have a subobject of class\tcode{Derived} which in turn will have a subobject of class\tcode{Base}.\exitexample\pnumA \grammarterm{base-specifier} followed by an ellipsis is a packexpansion~(\ref{temp.variadic}).\pnumThe order in which the base class subobjects are allocated in the mostderived object~(\ref{intro.object}) is unspecified.\enternote\indextext{directed~acyclic~graph|see{DAG}}%\indextext{lattice|see{DAG, subobject}}%a derived class and its base class subobjects can be represented by adirected acyclic graph (DAG) where an arrow means directly derivedfrom.'' A DAG of subobjects is often referred to as a subobjectlattice.''\begin{importgraphic}{Directed acyclic graph}{fig:dag}{figdag.pdf}\end{importgraphic}\pnumThe arrows need not have a physical representation in memory.\exitnote\pnum\enternoteInitialization of objects representing base classes can be specified inconstructors; see~\ref{class.base.init}.\exitnote\pnum\enternoteA base class subobject might have a layout~(\ref{basic.stc}) differentfrom the layout of a most derived object of the same type. A base classsubobject might have a polymorphic behavior~(\ref{class.cdtor})different from the polymorphic behavior of a most derived object of thesame type. A base class subobject may be of zero size (Clause~\ref{class});however, two subobjects that have the same class type and that belong tothe same most derived object must not be allocated at the sameaddress~(\ref{expr.eq}).\exitnote\rSec1[class.mi]{Multiple base classes}\indextext{multiple~inheritance}%\indextext{base~class}%\pnumA class can be derived from any number of base classes.\enternoteThe use of more than one direct base class is often called multiple inheritance.\exitnote\enterexample\begin{codeblock}class A { /* ... */ };class B { /* ... */ };class C { /* ... */ };class D : public A, public B, public C { /* ... */ };\end{codeblock}\exitexample\pnum\indextext{layout!class~object}%\indextext{initialization!order~of}%\enternoteThe order of derivation is not significant except as specified by thesemantics of initialization by constructor~(\ref{class.base.init}),cleanup~(\ref{class.dtor}), and storagelayout~(\ref{class.mem},~\ref{class.access.spec}).\exitnote\pnumA class shall not be specified as a direct base class of a derived classmore than once.\enternoteA class can be an indirect base class more than once and can be a directand an indirect base class. There are limited things that can be donewith such a class. The non-static data members and member functions ofthe direct base class cannot be referred to in the scope of the derivedclass. However, the static members, enumerations and types can beunambiguously referred to.\exitnote\enterexample\begin{codeblock}class X { /* ... */ };class Y : public X, public X { /* ... */ }; // ill-formed\end{codeblock}\begin{codeblock}class L { public: int next; /* ... */ };class A : public L { /* ... */ };class B : public L { /* ... */ };class C : public A, public B { void f(); /* ... */ }; // well-formedclass D : public A, public L { void f(); /* ... */ }; // well-formed\end{codeblock}\exitexample\pnum\indextext{virtual~base~class}%A base class specifier that does not contain the keyword\tcode{virtual}, specifies a \grammarterm{non-virtual} base class. A baseclass specifier that contains the keyword \tcode{virtual}, specifies a\term{virtual} base class. For each distinct occurrence of anon-virtual base class in the class lattice of the most derived class,the most derived object~(\ref{intro.object}) shall contain acorresponding distinct base class subobject of that type. For eachdistinct base class that is specified virtual, the most derived objectshall contain a single base class subobject of that type.\enterexamplefor an object of class type \tcode{C}, each distinct occurrence of a(non-virtual) base class \tcode{L} in the class lattice of \tcode{C}corresponds one-to-one with a distinct \tcode{L} subobject within theobject of type \tcode{C}. Given the class \tcode{C} defined above, anobject of class \tcode{C} will have two subobjects of class \tcode{L} asshown below.\begin{importgraphic}{Non-virtual base}{fig:nonvirt}{fignonvirt.pdf}\end{importgraphic}\pnumIn such lattices, explicit qualification can be used to specify whichsubobject is meant. The body of function \tcode{C::f} could refer to themember \tcode{next} of each \tcode{L} subobject:\begin{codeblock}void C::f() { A::next = B::next; } // well-formed\end{codeblock}Without the \tcode{A::} or \tcode{B::} qualifiers, the definition of\tcode{C::f} above would be ill-formed because ofambiguity~(\ref{class.member.lookup}).\pnumFor another example,\begin{codeblock}class V { /* ... */ };class A : virtual public V { /* ... */ };class B : virtual public V { /* ... */ };class C : public A, public B { /* ... */ };\end{codeblock}for an object \tcode{c} of class type \tcode{C}, a single subobject oftype \tcode{V} is shared by every base subobject of \tcode{c} that has a\tcode{virtual} base class of type \tcode{V}. Given the class \tcode{C}defined above, an object of class \tcode{C} will have one subobject ofclass \tcode{V}, as shown below.\indextext{DAG!multiple~inheritance}%\indextext{DAG!virtual~base~class}%\begin{importgraphic}{Virtual base}{fig:virt}{figvirt.pdf}\end{importgraphic}\pnumA class can have both virtual and non-virtual base classes of a giventype.\begin{codeblock}class B { /* ... */ };class X : virtual public B { /* ... */ };class Y : virtual public B { /* ... */ };class Z : public B { /* ... */ };class AA : public X, public Y, public Z { /* ... */ };\end{codeblock}For an object of class \tcode{AA}, all \tcode{virtual} occurrences ofbase class \tcode{B} in the class lattice of \tcode{AA} correspond to asingle \tcode{B} subobject within the object of type \tcode{AA}, andevery other occurrence of a (non-virtual) base class \tcode{B} in theclass lattice of \tcode{AA} corresponds one-to-one with a distinct\tcode{B} subobject within the object of type \tcode{AA}. Given theclass \tcode{AA} defined above, class \tcode{AA} has two subobjects ofclass \tcode{B}: \tcode{Z}'s \tcode{B} and the virtual \tcode{B} sharedby \tcode{X} and \tcode{Y}, as shown below.\indextext{DAG!virtual~base~class}%\indextext{DAG!non-virtual~base~class}%\indextext{DAG!multiple~inheritance}%\begin{importgraphic}{Virtual and non-virtual base}{fig:virtnonvirt}{figvirtnonvirt.pdf}\end{importgraphic}\exitexample\rSec1[class.member.lookup]{Member name lookup}%\indextext{lookup!member name}%\indextext{ambiguity!base~class~member}%\indextext{ambiguity!member access}\pnumMember name lookup determines the meaning of a name(\grammarterm{id-expression}) in a class scope~(\ref{basic.scope.class}).Name lookup can result in an \term{ambiguity}, in which case theprogram is ill-formed. For an \grammarterm{id-expression}, name lookupbegins in the class scope of \tcode{this}; for a\grammarterm{qualified-id}, name lookup begins in the scope of the\grammarterm{nested-name-specifier}. Name lookup takes place before accesscontrol~(\ref{basic.lookup}, Clause~\ref{class.access}).\pnumThe following steps define the result of name lookup for a member name\tcode{f} in a class scope \tcode{C}.\pnumThe \term{lookup set} for \tcode{f} in \tcode{C}, called $S(f,C)$,consists of two component sets: the \term{declaration set}, a set ofmembers named \tcode{f}; and the \term{subobject set}, a set ofsubobjects where declarations of these members (possibly including\grammarterm{using-declaration}{s}) were found. In the declaration set,\grammarterm{using-declaration}{s} are replaced by the members theydesignate, and type declarations (including injected-class-names) arereplaced by the types they designate. $S(f,C)$ is calculated as follows:\pnumIf \tcode{C} contains a declaration of the name \tcode{f}, thedeclaration set contains every declaration of \tcode{f} declared in\tcode{C} that satisfies the requirements of the language construct inwhich the lookup occurs.\enternoteLooking up a name in an\grammarterm{elaborated-type-specifier}~(\ref{basic.lookup.elab}) or\grammarterm{base-specifier} (Clause~\ref{class.derived}), for instance,ignores all non-type declarations, while looking up a name in a\grammarterm{nested-name-specifier}~(\ref{basic.lookup.qual}) ignoresfunction, variable, and enumerator declarations. As another example,looking up a name in a\grammarterm{using-declaration}~(\ref{namespace.udecl}) includes thedeclaration of a class or enumeration that would ordinarily be hidden byanother declaration of that name in the same scope.\exitnoteIf the resulting declaration set is not empty, the subobject setcontains \tcode{C} itself, and calculation is complete.\pnumOtherwise (i.e., \tcode{C} does not contain a declaration of \tcode{f}or the resulting declaration set is empty), $S(f,C)$ is initially empty.If \tcode{C} has base classes, calculate the lookup set for \tcode{f} ineach direct base class subobject $B_i$, and merge each such lookup set$S(f,B_i)$ in turn into $S(f,C)$.\pnumThe following steps define the result of merging lookup set $S(f,B_i)$into the intermediate $S(f,C)$:\begin{itemize}\item If each of the subobject members of $S(f,B_i)$ is a base classsubobject of at least one of the subobject members of $S(f,C)$, or if$S(f,B_i)$ is empty, $S(f,C)$ is unchanged and the merge is complete.Conversely, if each of the subobject members of $S(f,C)$ is a base classsubobject of at least one of the subobject members of $S(f,B_i)$, or if$S(f,C)$ is empty, the new $S(f,C)$ is a copy of $S(f,B_i)$.\item Otherwise, if the declaration sets of $S(f,B_i)$ and $S(f,C)$differ, the merge is ambiguous: the new $S(f,C)$ is a lookup set with aninvalid declaration set and the union of the subobject sets. Insubsequent merges, an invalid declaration set is considered differentfrom any other.\item Otherwise, the new $S(f,C)$ is a lookup set with the shared set ofdeclarations and the union of the subobject sets.\end{itemize}\pnumThe result of name lookup for \tcode{f} in \tcode{C} is the declarationset of $S(f,C)$. If it is an invalid set, the program is ill-formed.\enterexample\begin{codeblock}struct A { int x; }; // S(x,A) = \{ \{ \tcode{A::x} \}, \{ \tcode{A} \} \}struct B { float x; }; // S(x,B) = \{ \{ \tcode{B::x} \}, \{ \tcode{B} \} \}struct C: public A, public B { }; // S(x,C) = \{ invalid, \{ \tcode{A} in \tcode{C}, \tcode{B} in \tcode{C} \} \}struct D: public virtual C { }; // S(x,D) = S(x,C)struct E: public virtual C { char x; }; // S(x,E) = \{ \{ \tcode{E::x} \}, \{ \tcode{E} \} \}struct F: public D, public E { }; // S(x,F) = S(x,E)int main() {  F f;  f.x = 0; // OK, lookup finds \tcode{E::x}}\end{codeblock}$S(x,F)$ is unambiguous because the \tcode{A} and \tcode{B} basesubobjects of \tcode{D} are also base subobjects of \tcode{E}, so$S(x,D)$ is discarded in the first merge step.\exitexample\pnum\indextext{access~control!overloading~resolution~and}%If the name of an overloaded function is unambiguously found,overloading resolution~(\ref{over.match}) also takes place before accesscontrol.\indextext{example!scope~resolution operator}%\indextext{example!explicit~qualification}%\indextext{overloading!resolution!scoping ambiguity}%Ambiguities can often be resolved by qualifying a name with its class name.\enterexample\begin{codeblock}struct A {  int f();};\end{codeblock}\begin{codeblock}struct B {  int f();};\end{codeblock}\begin{codeblock}struct C : A, B {  int f() { return A::f() + B::f(); }};\end{codeblock}\exitexample\pnum\enternoteA static member, a nested type or an enumerator defined in a base class\tcode{T} can unambiguously be found even if an object has more than onebase class subobject of type \tcode{T}. Two base class subobjects sharethe non-static member subobjects of their common virtual base classes.\exitnote\enterexample\begin{codeblock}struct V {  int v;};struct A {  int a;  static int s;  enum { e };};struct B : A, virtual V { };struct C : A, virtual V { };struct D : B, C { };void f(D* pd) {  pd->v++; // OK: only one \tcode{v} (virtual)  pd->s++; // OK: only one \tcode{s} (static)  int i = pd->e; // OK: only one \tcode{e} (enumerator)  pd->a++; // error, ambiguous: two \tcode{a}{s} in \tcode{D}}\end{codeblock}\exitexample\pnum\enternote\indextext{dominance!virtual~base~class}%When virtual base classes are used, a hidden declaration can be reachedalong a path through the subobject lattice that does not pass throughthe hiding declaration. This is not an ambiguity. The identical use withnon-virtual base classes is an ambiguity; in that case there is nounique instance of the name that hides all the others.\exitnote\enterexample\begin{codeblock}struct V { int f(); int x; };struct W { int g(); int y; };struct B : virtual V, W {  int f(); int x;  int g(); int y;};struct C : virtual V, W { };struct D : B, C { void glorp(); };\end{codeblock}\begin{importgraphic}{Name lookup}{fig:name}{figname.pdf}\end{importgraphic}\pnum\enternoteThe names declared in \tcode{V} and the left-hand instance of \tcode{W}are hidden by those in \tcode{B}, but the names declared in theright-hand instance of \tcode{W} are not hidden at all.\exitnote\begin{codeblock}void D::glorp() {  x++; // OK: \tcode{B::x} hides \tcode{V::x}  f(); // OK: \tcode{B::f()} hides \tcode{V::f()}  y++; // error: \tcode{B::y} and \tcode{C}'s \tcode{W::y}  g(); // error: \tcode{B::g()} and \tcode{C}'s \tcode{W::g()}}\end{codeblock}\exitexample\indextext{ambiguity!class conversion}%\pnumAn explicit or implicit conversion from a pointer to oran expression designating an objectof aderived class to a pointer or reference to one of its base classes shallunambiguously refer to a unique object representing the base class.\enterexample\begin{codeblock}struct V { };struct A { };struct B : A, virtual V { };struct C : A, virtual V { };struct D : B, C { };void g() {  D d;  B* pb = &d;  A* pa = &d; // error, ambiguous: \tcode{C}'s \tcode{A} or \tcode{B}'s \tcode{A}?  V* pv = &d; // OK: only one \tcode{V} subobject}\end{codeblock}\exitexample\pnum\enternoteEven if the result of name lookup is unambiguous, use of a name found inmultiple subobjects might still beambiguous~(\ref{conv.mem},~\ref{expr.ref}, \ref{class.access.base}).\exitnote\enterexample\begin{codeblock}struct B1 {  void f();  static void f(int);  int i;};struct B2 {  void f(double);};struct I1: B1 { };struct I2: B1 { };struct D: I1, I2, B2 {  using B1::f;  using B2::f;  void g() {    f(); // Ambiguous conversion of \tcode{this}    f(0); // Unambiguous (static)    f(0.0); // Unambiguous (only one \tcode{B2})    int B1::* mpB1 = &D::i; // Unambiguous    int D::* mpD = &D::i; // Ambiguous conversion  }};\end{codeblock}\exitexample\rSec1[class.virtual]{Virtual functions}%\indextext{virtual~function|(}%\indextext{type!polymorphic}%\indextext{class!polymorphic}\pnumVirtual functions support dynamic binding and object-orientedprogramming. A class that declares or inherits a virtual function iscalled a \term{polymorphic class}.\pnumIf a virtual member function \tcode{vf} is declared in a class\tcode{Base} and in a class \tcode{Derived}, derived directly orindirectly from \tcode{Base}, a member function \tcode{vf} with the samename, parameter-type-list~(\ref{dcl.fct}), cv-qualification, and ref-qualifier(or absence of same) as\tcode{Base::vf} is declared, then \tcode{Derived::vf} is also virtual(whether or not it is so declared) and it \term{overrides}\footnote{A function with the same name but a different parameter list(Clause~\ref{over}) as a virtual function is not necessarily virtual anddoes not override. The use of the \tcode{virtual} specifier in thedeclaration of an overriding function is legal but redundant (has emptysemantics). Access control (Clause~\ref{class.access}) is not considered indetermining overriding.}\tcode{Base::vf}. For convenience we say that any virtual functionoverrides itself.\indextext{overrider!final}%A virtual member function \tcode{C::vf} of a class object \tcode{S} is a \defn{finaloverrider} unless the most derived class~(\ref{intro.object}) of which \tcode{S} is abase class subobject (if any) declares or inherits another member function that overrides\tcode{vf}. In a derived class, if a virtual member function of a base class subobjecthas more than one final overrider the program is ill-formed.\enterexample\begin{codeblock}struct A {  virtual void f();};struct B : virtual A {  virtual void f();};struct C : B , virtual A {  using A::f;};void foo() {  C c;  c.f(); // calls \tcode{B::f}, the final overrider  c.C::f(); // calls \tcode{A::f} because of the using-declaration}\end{codeblock}\exitexample\enterexample\begin{codeblock}struct A { virtual void f(); };struct B : A { };struct C : A { void f(); };struct D : B, C { }; // OK: \tcode{A::f} and \tcode{C::f} are the final overriders                      // for the \tcode{B} and \tcode{C} subobjects, respectively\end{codeblock}\exitexample\pnum\enternoteA virtual member function does not have to be visible to be overridden,for example,\begin{codeblock}struct B {  virtual void f();};struct D : B {  void f(int);};struct D2 : D {  void f();};\end{codeblock}the function \tcode{f(int)} in class \tcode{D} hides the virtualfunction \tcode{f()} in its base class \tcode{B}; \tcode{D::f(int)} isnot a virtual function. However, \tcode{f()} declared in class\tcode{D2} has the same name and the same parameter list as\tcode{B::f()}, and therefore is a virtual function that overrides thefunction \tcode{B::f()} even though \tcode{B::f()} is not visible inclass \tcode{D2}.\exitnote\pnumIf a virtual function \tcode{f} in some class \tcode{B} is marked with the\grammarterm{virt-specifier} \tcode{final} and in a class \tcode{D} derived from \tcode{B}a function \tcode{D::f} overrides \tcode{B::f}, the program is ill-formed. \enterexample\begin{codeblock}struct B {  virtual void f() const final;};struct D : B {  void f() const; // error: \tcode{D::f} attempts to override \tcode{final} \tcode{B::f}};\end{codeblock}\exitexample\pnumIf a virtual function is marked with the \grammarterm{virt-specifier} \tcode{override} anddoes not override a member function of a base class, the program is ill-formed. \enterexample\begin{codeblock}struct B {  virtual void f(int);};struct D : B {  virtual void f(long) override; // error: wrong signature overriding \tcode{B::f}  virtual void f(int) override; // OK};\end{codeblock}\exitexample\pnumEven though destructors are not inherited, a destructor in a derivedclass overrides a base class destructor declared virtual;see~\ref{class.dtor} and~\ref{class.free}.\pnumThe return type of an overriding function shall be either identical tothe return type of the overridden function or \term{covariant} withthe classes of the functions. If a function \tcode{D::f} overrides afunction \tcode{B::f}, the return types of the functions are covariantif they satisfy the following criteria:\begin{itemize}\item both are pointers to classes, both are lvalue references toclasses, or both are rvalue references to classes\footnote{Multi-level pointers to classes or references to multi-level pointers toclasses are not allowed.% }\item the class in the return type of \tcode{B::f} is the same class asthe class in the return type of \tcode{D::f}, or is an unambiguous andaccessible direct or indirect base class of the class in the return typeof \tcode{D::f}\item both pointers or references have the same cv-qualification and theclass type in the return type of \tcode{D::f} has the samecv-qualification as or less cv-qualification than the class type in thereturn type of \tcode{B::f}.\end{itemize}\pnumIf the class type in the covariant return type of \tcode{D::f} differs from that of\tcode{B::f}, the class type in the return type of \tcode{D::f} shall becomplete at the point of declaration of \tcode{D::f} or shall be theclass type \tcode{D}. When the overriding function is called as thefinal overrider of the overridden function, its result is converted tothe type returned by the (statically chosen) overriddenfunction~(\ref{expr.call}).\enterexample\indextext{example!virtual~function}%\begin{codeblock}class B { };class D : private B { friend class Derived; };struct Base {  virtual void vf1();  virtual void vf2();  virtual void vf3();  virtual B* vf4();  virtual B* vf5();  void f();};struct No_good : public Base {  D* vf4(); // error: \tcode{B} (base class of \tcode{D}) inaccessible};class A;struct Derived : public Base {    void vf1(); // virtual and overrides \tcode{Base::vf1()}    void vf2(int); // not virtual, hides \tcode{Base::vf2()}    char vf3(); // error: invalid difference in return type only    D* vf4(); // OK: returns pointer to derived class    A* vf5(); // error: returns pointer to incomplete class    void f();};void g() {  Derived d;  Base* bp = &d; // standard conversion:                                // \tcode{Derived*} to \tcode{Base*}  bp->vf1(); // calls \tcode{Derived::vf1()}  bp->vf2(); // calls \tcode{Base::vf2()}  bp->f(); // calls \tcode{Base::f()} (not virtual)  B* p = bp->vf4(); // calls \tcode{Derived::pf()} and converts the                                // result to \tcode{B*}  Derived* dp = &d;  D* q = dp->vf4(); // calls \tcode{Derived::pf()} and does not                                // convert the result to \tcode{B*}  dp->vf2(); // ill-formed: argument mismatch}\end{codeblock}\exitexample\pnum\enternoteThe interpretation of the call of a virtual function depends on the typeof the object for which it is called (the dynamic type), whereas theinterpretation of a call of a non-virtual member function depends onlyon the type of the pointer or reference denoting that object (the statictype)~(\ref{expr.call}).\exitnote\pnum\enternoteThe \tcode{virtual} specifier implies membership, so a virtual functioncannot be a nonmember~(\ref{dcl.fct.spec}) function. Nor can a virtualfunction be a static member, since a virtual function call relies on aspecific object for determining which function to invoke. A virtualfunction declared in one class can be declared a \tcode{friend} inanother class.\exitnote\pnum\indextext{definition!virtual~function}%A virtual function declared in a class shall be defined, or declaredpure~(\ref{class.abstract}) in that class, or both; but no diagnostic isrequired~(\ref{basic.def.odr}).\indextext{friend!\tcode{virtual}~and}%\pnum\indextext{multiple~inheritance!\tcode{virtual}~and}%\enterexamplehere are some uses of virtual functions with multiple base classes:\indextext{example!virtual~function}%\begin{codeblock}struct A {  virtual void f();};struct B1 : A { // note non-virtual derivation  void f();};struct B2 : A {  void f();};struct D : B1, B2 { // \tcode{D} has two separate \tcode{A} subobjects};void foo() {  D d;//\tcode{ A* ap = \&d;} // would be ill-formed: ambiguous  B1* b1p = &d;  A* ap = b1p;  D* dp = &d;  ap->f(); // calls \tcode{D::B1::f}  dp->f(); // ill-formed: ambiguous}\end{codeblock}In class \tcode{D} above there are two occurrences of class \tcode{A}and hence two occurrences of the virtual member function \tcode{A::f}.The final overrider of \tcode{B1::A::f} is \tcode{B1::f} and the finaloverrider of \tcode{B2::A::f} is \tcode{B2::f}.\pnumThe following example shows a function that does not have a unique finaloverrider:\begin{codeblock}struct A {  virtual void f();};struct VB1 : virtual A { // note virtual derivation  void f();};struct VB2 : virtual A {  void f();};struct Error : VB1, VB2 { // ill-formed};struct Okay : VB1, VB2 {  void f();};\end{codeblock}Both \tcode{VB1::f} and \tcode{VB2::f} override \tcode{A::f} but thereis no overrider of both of them in class \tcode{Error}. This example istherefore ill-formed. Class \tcode{Okay} is well formed, however,because \tcode{Okay::f} is a final overrider.\pnumThe following example uses the well-formed classes from above.\begin{codeblock}struct VB1a : virtual A { // does not declare \tcode{f}};struct Da : VB1a, VB2 {};void foe() {  VB1a* vb1ap = new Da;  vb1ap->f(); // calls \tcode{VB2::f}}\end{codeblock}\exitexample\pnum\indextext{operator!scope~resolution}%\indextext{virtual~function~call}%Explicit qualification with the scope operator~(\ref{expr.prim})suppresses the virtual call mechanism.\enterexample\begin{codeblock}class B { public: virtual void f(); };class D : public B { public: void f(); };void D::f() { /* ... */ B::f(); }\end{codeblock}Here, the function call in\tcode{D::f}really does call\tcode{B::f}and not\tcode{D::f}.\exitexample\pnumA function with a deleted definition~(\ref{dcl.fct.def}) shallnot override a function that does not have a deleted definition. Likewise,a function that does not have a deleted definition shall not override afunction with a deleted definition.%\indextext{virtual~function|)}\rSec1[class.abstract]{Abstract classes}%\indextext{class!abstract}\pnumThe abstract class mechanism supports the notion of a general concept,such as a \tcode{shape}, of which only more concrete variants, such as\tcode{circle} and \tcode{square}, can actually be used. An abstractclass can also be used to define an interface for which derived classesprovide a variety of implementations.\pnumAn \term{abstract class} is a class that can be used onlyas a base class of some other class; no objects of an abstract class canbe created except as subobjects of a class derived from it. A class isabstract if it has at least one \term{pure virtual function}.\enternoteSuch a function might be inherited: see below.\exitnote\indextext{virtual~function!pure}%A virtual function is specified \term{pure} by using a\grammarterm{pure-specifier}~(\ref{class.mem}) in the function declarationin the class definition.\indextext{definition!pure virtual~function}%A pure virtual function need be defined only if called with, or as ifwith~(\ref{class.dtor}), the \grammarterm{qualified-id}syntax~(\ref{expr.prim}).\enterexample\indextext{example!pure virtual~function}%\begin{codeblock}class point { /* ... */ };class shape { // abstract class  point center;public:  point where() { return center; }  void move(point p) { center=p; draw(); }  virtual void rotate(int) = 0; // pure virtual  virtual void draw() = 0; // pure virtual};\end{codeblock}\exitexample\enternoteA function declaration cannot provide both a \grammarterm{pure-specifier}and a definition\exitnote\enterexample\begin{codeblock}struct C {  virtual void f() = 0 { }; // ill-formed};\end{codeblock}\exitexample\pnum\indextext{class!pointer~to abstract}%An abstract class shall not be used as a parameter type, as a functionreturn type, or as the type of an explicit conversion. Pointers andreferences to an abstract class can be declared.\enterexample\begin{codeblock}shape x; // error: object of abstract classshape* p; // OKshape f(); // errorvoid g(shape); // errorshape& h(shape&); // OK\end{codeblock}\exitexample\pnum\indextext{virtual~function!pure}%A class is abstract if it contains or inherits at least one pure virtualfunction for which the final overrider is pure virtual.\enterexample\begin{codeblock}class ab_circle : public shape {  int radius;public:  void rotate(int) { }  // \tcode{ab_circle::draw()} is a pure virtual};\end{codeblock}Since \tcode{shape::draw()} is a pure virtual function\tcode{ab_circle::draw()} is a pure virtual by default. The alternativedeclaration,\begin{codeblock}class circle : public shape {  int radius;public:  void rotate(int) { }  void draw(); // a definition is required somewhere};\end{codeblock}would make class \tcode{circle} nonabstract and a definition of\tcode{circle::draw()} must be provided.\exitexample\pnum\enternoteAn abstract class can be derived from a class that is not abstract, anda pure virtual function may override a virtual function which is notpure.\exitnote\pnum\indextext{class!constructor~and abstract}%Member functions can be called from a constructor (or destructor) of anabstract class;\indextext{virtual~function~call!undefined pure}%the effect of making a virtual call~(\ref{class.virtual}) to a purevirtual function directly or indirectly for the object being created (ordestroyed) from such a constructor (or destructor) is undefined.%\indextext{derived~class|)}
Something went wrong with that request. Please try again.