为什么 C++11 允许抽象的最终类

Why does C++11 allow abstract final classes?

本文关键字:抽象的 C++11 为什么      更新时间:2023-10-16

为什么编译下面的代码?我正在使用视觉工作室;我不确定它是否只是不符合标准,或者是否有充分的理由允许这样做,或者它只是语言中的疏忽。

struct Base {
  virtual void foo() = 0;
};
// since this class is final and abstract, it can never be
// instantiated - why isn't its very declaration an error?
struct Derived final : Base {};
int main() {
  //Derived derived; // this IS an error, but relies on someone trying
                     // to instantiate the class, and the error is at the site
                     // of instantiation, not the class itself
}

因为没有充分的理由禁止它。它似乎不是很有用,但是如果您想要一个无法实例化但仍提供静态成员函数的结构,这是实现此目的的一种方法。(私有构造函数可能是实现此目的的更好方法。

让Java和朋友们来取缔这种事情:为什么公共抽象最终类在语法上无效?