How can I tell the inheriting class to not call its base class' parameter-less constructor?

前端 未结 7 2619
礼貌的吻别
礼貌的吻别 2021-02-19 23:32

I was surprised to find out that the parameter-less constructor of my base class is called any time I call any constructor in a derived class. I thought that is what

7条回答
  •  既然无缘
    2021-02-20 00:28

    The only way is to explicitly tell it which other base ctor you want it to call; which of course means you must choose some base ctor to call.

    You can't have it call no base ctor at all - conceptually, constructing a Customer is done by first constructing a Person, and then doing Customer specific construction on top of it. For example, suppose Person had private fields - how would these be correctly constructed if construction of a Customer was allowed to not first construct a Person?

提交回复
热议问题