dimanche 6 septembre 2020

How to enforce overriding a virtual function in subclasses

Let's imagine I have the following code:

class Base {
  virtual void myFunc();
};

class Subclass1 : public Base { // OK
  virtual void myFunc() override; // overrides the base function
};

class Subclass2 : public Base { // How to make this fail ?
  // does not declare an override for myFunc
};

Is there any way in C++ to make compilation fail because Subclass2 does not implement its variant of myFunc() ? I know how to make the opposite, that is to make compilation fail because Subclass1 overrides myFunc() (by using final), but I'm not aware of any mechanic to achieve what I want in C++11.

I'm curious about possibilities in later C++ standards as well.

Aucun commentaire:

Enregistrer un commentaire