博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
构造函数不能被继承的原因
阅读量:7141 次
发布时间:2019-06-28

本文共 2766 字,大约阅读时间需要 9 分钟。

If constructors were inherited in C++, it would cause many undesirable problems, and the main benefit of inheritance of members would not apply to constructors.

Here are some problems if constructors were inherited in C++.

  • Subclasses are forced to support all the methods of construction of superclasses. A subclass often implements a more specific specialization of the parent class. But if constructors were inherited, then that means it must be also possible to construct the subclass using any constructor of the superclass, even ones that may be too general to be applicable to this particular subclass. The only way for the subclass to deal with this is to explicitly override the unwanted constructors, and throw an error or something; this is a bad solution because it is not checked at compile time, only detected at runtime. Or, the language needs to add a new construct for the class declaration to "un-inherit" a constructor; which is also messy.
  • C++ has multiple inheritance. If a class inherits from multiple classes, would it inherit constructors from both parent classes? If both parent classes have a constructor with the same signature, how would that be resolved?
  • A constructor in C++ has an "initializer list" where it provides arguments for the construction of the base class(es) and fields. A base class or field not explicitly specified in the initializer list is default constructed (i.e. constructed with a constructor of no parameters). If such an omitted base class or field type is not default-constructible, the constructor does not compile. However, if constructors were inherited, the user would not get to specify the initializer list for that constructor for the derived class. What if there are fields in that derived class which are not default-constructible? How can this problem be prevented? Disallow all derived classes from having non-default-constructible fields? That would be way too restrictive.

More importantly, the main benefit of inheritance do not apply to constructors:

    • The main benefit of inheritance is subtype polymorphism. If you have a pointer or a reference to a base class type, it can point to an object of that type or any derived type. You can access fields and virtual methods using that pointer/reference to base class type, and it will work even if the pointer points to an object of derived class type, without you knowing, or caring, what that type actually is. However, for constructors, this is not relevant, because any place where a constructor is invoked, you explicitly specify the class name to be constructed. There is no situation where you could call a constructor on an object that could possibly be different types; it wouldn't make sense.

转载于:https://www.cnblogs.com/Key-Ky/p/7469264.html

你可能感兴趣的文章
sass的基本使用
查看>>
chrome扩展推荐:帮你留住每一次ctrl+c --- Clipboard History 2
查看>>
恶意软件盯上了加密货币,两家以色列公司受到攻击
查看>>
专访《Haskell函数式编程入门》作者张淞:浅谈Haskell的优点与启发
查看>>
VS2017 15.4提供预览版,面向Windows 10秋季更新(FCU)
查看>>
Spring Web Services 3.0.4.RELEASE和2.4.3.RELEASE发布
查看>>
如何自动搞定全站图片的alt属性?
查看>>
配置一次,到处运行:将配置与运行时解耦
查看>>
突发热点事件下微博高可用注册中心vintage的设计\u0026实践
查看>>
Elixir 1.3带来新的语言功能、API和改进后的工具
查看>>
用Elm语言降低失败的风险
查看>>
抓住热门话题一对一直播,如何在风浪四起的直播市场劈风斩浪? ...
查看>>
手把手教你用owncloud搭建属于自己的云盘
查看>>
epoll+socket实现 socket并发 linux服务器
查看>>
阿里巴巴人事再调整,将打通淘宝、天猫两个消费场景 ...
查看>>
Kubernetes + CRI + Kata + Firecracker
查看>>
菜鸟成都未来园区启动,无人车首次进入园区调拨运输环节 ...
查看>>
算法不扎实的程序员,每个都很慌
查看>>
4个需要避免的常见Kubernetes监控陷阱
查看>>
规划一个智能工厂应避免的十个坑
查看>>