class MyInterface { public : virtual ~MyInterface() {} protected : // With C++11, these methods would be "= default" MyInterface() {} MyInterface(const MyInterface & ) {} MyInterface & operator = (const MyInterface & ) { return *this ; } MyInterface(MyInterface && ) noexcept {} MyInterface & operator = (MyInterface && ) noexcept { return *this ; } } ;
#define DECLARE_CLASS_AS_INTERFACE(ClassName) \ public : \ virtual ~ClassName() {} \ protected : \ ClassName() {} \ ClassName(const ClassName & ) {} \ ClassName & operator = (const ClassName & ) { return *this ; } \ ClassName(ClassName && ) noexcept {} \ ClassName & operator = (ClassName && ) noexcept { return *this ; } \ private :
- This class cannot be instantiated (the constructors are protected)
- This class can be virtually destroyed
- This class can be inherited without imposing undue constraints on inheriting classes (e.g. the inheriting class could be by default copiable/moveable)
- The use of the macro means the interface "declaration" is easily recognizable (and searchable), and its code is factored in one place making it easier to modify (a suitably prefixed name will remove undesirable name clashes)