首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >为什么C++11允许抽象的最终类?

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

Stack Overflow用户
提问于 2014-02-16 02:04:01
回答 1查看 991关注 0票数 1

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

代码语言:javascript
运行
复制
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
}
EN

回答 1

Stack Overflow用户

发布于 2022-01-21 16:42:16

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

让Java和朋友来宣布这类事情为非法:Why is public abstract final class syntactically invalid?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21806339

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档