I would like to get the stackoverflow community's opinion on the following three design patterns. The first is implementation inheritance; the second is interface inheritance; the third is a middle ground. My specific question is: Which is best?
implementation inheritance:
class Base {
X x() const = 0;
void UpdateX(A a) { y_ = g(a); }
Y y_;
}
class Derived: Base {
X x() const { return f(y_); }
}
interface inheritance:
class Base {
X x() const = 0;
void UpdateX(A a) = 0;
}
class Derived: Base {
X x() const { return x_; }
void UpdateX(A a) { x_ = f(g(a)); }
X x_;
}
middle ground:
class Base {
X x() const { return x_; }
void UpdateX(A a) = 0;
X x_;
}
class Derived: Base {
void UpdateX(A a) { x_ = f(g(a)); }
}
I know that many people prefer interface inheritance to implementation inheritance. However, the advantage of the latter is that with a pointer to Base
, x() can be inlined and the address of x_
can be statically calculated.