C++ 为什么C++;11允许抽象的最终类?
为什么要编译下面的代码?我正在使用VisualStudio;我不确定它是否只是不符合标准,或者是否有充分的理由允许这样做,或者它只是语言中的一个疏忽C++ 为什么C++;11允许抽象的最终类?,c++,c++11,C++,C++11,为什么要编译下面的代码?我正在使用VisualStudio;我不确定它是否只是不符合标准,或者是否有充分的理由允许这样做,或者它只是语言中的一个疏忽 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 Derive
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
}
我能想到的唯一一件事是,因为
静态
成员仍然是相关的。详细说明:因为派生的
静态成员和朋友
可以访问基本
受保护的成员,因此,派生的
不一定完全无用。是否因为它没有用处就必须明确禁止它?@sth-no-“它没有用处,但没有其他理由禁止它”可能是一个有效的答案。我开始期待,尽管非常聪明的人想出了这些东西,并且有很好的理由禁止/允许这些东西——像这样的东西似乎很少被允许,但在任何情况下都是完全无用的。我要指出,没有什么能阻止一个人在某种反常的MPL结构中使用派生的。事实上,可以有一个巨大的继承树,它是从Base
分支出来的,以final
叶子作为终端,只用于它们的类型。仅仅因为我不想编写这样的代码并不意味着有人不会。