Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Updated exception numbering; NFC

...

Code Block
bgColor#ccccff
langcpp
class B {
  void seize_mine();
  void release_mine();
  
public:
  B() { seize_mine(); }
  virtual ~B() { release_mine(); }

protected:
  virtual void seize() { seize_mine(); }
  virtual void release() { release_mine(); }
};

class D : public B {
  void seize_mine();
  void release_mine();
  
public:
  D() { seize_mine(); }
  virtual ~D() { release_mine(); }

protected:
  void seize() override {
    B::seize();
    seize_mine();
  }
  
  void release() override {
    release_mine();
    B::release();
  }
};

Exceptions

OOP30OOP50-CPP-EX1: Since valid uses cases exist that involve calling (non-pure) virtual functions from the constructor of a class, it is permissible to call the virtual function with an explicitly qualified id. This signifies to code maintainers that the expected behavior is for the class under construction or destruction to be the final overrider for the function call.

Code Block
bgColor#ccccff
langcpp
class A {
  A() {
    // f();   // WRONG!
    A::f();   // okay
  }
  virtual void f();
};

OOP30OOP50-CPP-EX2: It is permissible to call a virtual function that has the final virt-specifier from a constructor or destructor, as in this example:

...