Implementation/interface inheritance design question.
Posted
by Neil G
on Stack Overflow
See other posts from Stack Overflow
or by Neil G
Published on 2010-04-21T18:56:35Z
Indexed on
2010/05/06
17:08 UTC
Read the original article
Hit count: 293
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.
© Stack Overflow or respective owner