Is there a way to indicate or state that a child class must reimplement a specific non-abstract method?

Here is what I mean. I have a class hierarchy:

class A {
   virtual int f() = 0;
};

class B : public A {
   int f() override {return 5;}
   void doSpecificStuff() {}
}

B- a self-contained class that can be used on its own. But he also has many descendants:

class C : public B {
   int f() override {return 171;}
}

Is there any way to make sure that I will not forget fto reimplement when subclassing B?

+4
source share
1 answer

This solution is inspired by @dyp's comment:

You can separate two duties B, namely: "provides B-style implementation " and "can be created".

class B_for_implementation : public A
{
  void doSpecificStuff();
};

class B_for_objects final : public B_for_implementation
{
  int f() override {return 5;}
};

class C final : public B_for_implementation
{
  int f() override {return 171;}
};

Autonomous objects will be created from B_for_objects, while other classes will be obtained from B_for_implementation.

+5

All Articles