Here's 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
is a self-sufficient class that can be used on its own. But it also has many descendants:
class C : public B {
int f() override {return 171;}
}
Is there any way to make sure that I won't forget to re-implement f
when subclassing B
?
This solution is inspired by @dyp's comment:
You could split the two responsibilities of B
, namely "provides B
-style implementation" and "can be instantiated."
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;}
};
Self-contained objects would be created from B_for_objects
, while other classes would derive from B_for_implementation
.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments