I have multiple classes inheriting from an interface. The header files of the derived classes are almost equal as each overrides all pure virtual functions:
struct IBase {
virtual void fooA() = 0;
virtual void fooB() = 0;
};
struct Derived1 : IBase {
void fooA() override;
void fooB() override;
};
struct Derived2 : IBase {
void fooA() override;
void fooB() override;
};
void Derived1::fooA() {
// implA1
}
void Derived1::fooB() {
// implB1
}
void Derived2::fooA() {
// implA2
}
void Derived2::fooB() {
// implB2
}
Can I somehow avoid copy pasting void fooA() override; void fooB() override;
for every derived class?
It is not possible to avoid copy pasting void fooA() override; void fooB() override;
for every derived class, under the assumption that their implementations are indeed different (as indicated by your comment // implA1
and // implA2
), without resorting to macros.
NOTE: If the implementations of Derived1::fooA
and Derived2::fooA
are identical, then it is possible to avoid the duplication.