C++ “Interfaces”

Bringing It All Together

  • C++ has a toolset for

    • Inheritance

    • Dynamic dispatch

  • Does not make any policies

  • Java does

  • Programmer’s choice how to use tools

  • ⟶ very easily incomprehensible code is created

  • ⟶ best practices!


Bringing it all together: an interface is defined like so …

  • Virtual destructor, with an empty implementation (remember, destructors cannot be abstract/pure virtual)

  • Only abstract/pure virtual methods

class Interface
    virtual ~Interface() {}
    virtual void method() const = 0;
  • Ideally nothing else

  • Programmers are invited to break the rules!

  • ⟶ Hybrid forms, e.g. providing little helper methods in the interface’s protected interface

  • ⟶ Personal taste, combined with respect for colleagues


C++11 brings features for derived classes: