我在 C++ 中有以下类层次结构:
class Base {
virtual void apply() = 0;
};
class Derived : public Base {
virtual void apply() {
// implementation here that uses derived_specialty
}
virtual void derived_specialty() = 0;
};
class Implementation : public Derived {
virtual void derived_specialty() {
// implementation
}
};
我想保证实现级别的类不提供它们自己的应用实现,并且它们只实现派生专业。有没有办法保证继承自 Derived 的类不会实现 apply,从而使用 Derived::apply 实现?我的理解是,在 C++ 中,基类中的虚拟方法在继承层次结构中一直是虚拟的,但是如果 C++ 中有任何技巧可以完成,我很想听听它们。
我总是对 C++ 允许的事情感到惊讶,所以我认为值得一问。:)