• 构造函数不能被继承的原因


    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.
  • 相关阅读:
    [offer_53-1] 在排序数组中查找数字 I (开启编辑看 i,j,m)
    window10 办公软件word、execel、ppt突然变得很卡顿如何解决?
    数组中第k大的数
    heapq 堆
    每日一题 482. 密钥格式化
    算法笔记Go!
    DFS与BFS的python实现
    无向图中找到长度为k的“链”
    无序数组中找一个比左边都大、右边都小的数
    SRM(空域富模型隐写分析)
  • 原文地址:https://www.cnblogs.com/Key-Ky/p/7469264.html
Copyright © 2020-2023  润新知