Copy constructor needs to call a method that depends on the object, but constructor can't be virtual

后端 未结 6 628
伪装坚强ぢ
伪装坚强ぢ 2021-01-20 19:24

I have an abstract base class with two inherited classes. In both these classes I define a virtual method that is used by the constructor. Now I need to create a copy constr

6条回答
  •  醉梦人生
    2021-01-20 20:17

    Virtual functions called during the constructor/destructor won't resolve to derived type. As stated by Scott Myers here -

    During base class construction, virtual functions never go down into derived classes. Instead, the object behaves as if it were of the base type.

    since you can't use virtual functions to call down from base classes during construction, you can compensate by having derived classes pass necessary construction information up to base class constructors instead.

提交回复
热议问题