为什么下面的代码会编译?我正在使用Visual;我不确定它是否只是不符合标准,或者是否有充分的理由允许这样做,或者它是否只是语言中的一个疏忽。
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
}发布于 2022-01-21 16:42:16
因为没有很好的理由禁止它。这似乎不太有用,但是如果您想要一个不能实例化但仍然提供静态成员函数的结构,这是实现这一目标的一种方法。(私有构造函数可能是实现这一目的的更好方法。)
让Java和朋友来宣布这类事情为非法:Why is public abstract final class syntactically invalid?
https://stackoverflow.com/questions/21806339
复制相似问题