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 Bโ::โf, the final overrider c.C::f(); // calls Aโ::โf because of the using-declaration }โ end example
struct A { virtual void f(); }; struct B : A { }; struct C : A { void f(); }; struct D : B, C { }; // OK: Aโ::โf and Cโ::โf are the final overriders // for the B and C subobjects, respectivelyโ end example
struct B { virtual void f(); }; struct D : B { void f(int); }; struct D2 : D { void f(); };the function f(int) in class D hides the virtual function f() in its base class B; Dโ::โf(int) is not a virtual function.
struct B { virtual void f() const final; }; struct D : B { void f() const; // error: Dโ::โf attempts to override final Bโ::โf };โ end example
struct B { virtual void f(int); }; struct D : B { virtual void f(long) override; // error: wrong signature overriding Bโ::โf virtual void f(int) override; // OK };โ end example
struct A { virtual void f() requires true; // error: virtual function cannot be constrained ([temp.constr.decl]) };โ end example
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: B (base class of D) inaccessible }; class A; struct Derived : public Base { void vf1(); // virtual and overrides Baseโ::โvf1() void vf2(int); // not virtual, hides 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: // Derived* to Base* bp->vf1(); // calls Derivedโ::โvf1() bp->vf2(); // calls Baseโ::โvf2() bp->f(); // calls Baseโ::โf() (not virtual) B* p = bp->vf4(); // calls Derivedโ::โvf4() and converts the // result to B* Derived* dp = &d; D* q = dp->vf4(); // calls Derivedโ::โvf4() and does not // convert the result to B* dp->vf2(); // error: argument mismatch }โ end example
struct A { virtual void f(); }; struct B1 : A { // note non-virtual derivation void f(); }; struct B2 : A { void f(); }; struct D : B1, B2 { // D has two separate A subobjects }; void foo() { D d; // A* ap = &d; // would be ill-formed: ambiguous B1* b1p = &d; A* ap = b1p; D* dp = &d; ap->f(); // calls Dโ::โB1โ::โf dp->f(); // error: ambiguous }
struct A { virtual void f(); }; struct VB1 : virtual A { // note virtual derivation void f(); }; struct VB2 : virtual A { void f(); }; struct Error : VB1, VB2 { // error }; struct Okay : VB1, VB2 { void f(); };