What is it that allows for pure virtual functions with definitions?

It can be used and is not a bug. This is a syntax that allows you to define an abstract class with default definitions for virtual functions. The function must still be defined in derived classes (with the exception(?) of destructors), but the derived class can choose to use the default definition. This allows you to accomplish this without having to write an "extra" pure virtual function solely to declare the class abstract.

For example:

#include <iostream>
#include <memory>

// Class is abstract...
struct Base {
  virtual void print() = 0;
  virtual ~Base() = default;
};

// But provides default definitions.
inline void Base::print() {
  std::cout << "Base::print()\n";
}

struct Derived : Base {
  virtual void print() override {
    Base::print();
    std::cout << "Derived::print()\n";
  }
};

int main() {
  auto base_ptr = std::unique_ptr<Base>(std::make_unique<Derived>());
  base_ptr->print();
}

Output is:

Base::print()
Derived::print()
/r/cpp_questions Thread Parent