Converting Derived** to Base** and Derived* to Base*

前端 未结 6 833

Ok, I was reading through this entry in the FQA dealing about the issue of converting a Derived** to a Base** and why it is forbidden, and I got th

6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-10 20:26

    No, nasty_function() isn't as nasty as it sounds. As the pointer b points to something that is-a Base, it's perfectly legal to assign a Base-value to it.

    Take care: your "Ooops" comment is not correct: d still points to the same Derived as before the call! Only, the Base part of it was reassigned (by value!). If that gets your whole Derived out of consistency, you need to redesign by making Base::operator=() virtual. Then, in the nasty_function(), in fact the Derived assignment operator will be called (if defined).

    So, I think, your example does not have that much to do with the pointer-to-pointer case.

提交回复
热议问题