Downcast in a diamond hierarchy

我只是一个虾纸丫 提交于 2019-12-03 10:39:42

Because if the object was actually of type E (derived from D), the location of A subobject relative to D subobject could be different than if the object is actually D.

It actually already happens if you consider instead casting from A to C. When you allocate C, it has to contain instance of A and it lives at some specific offset. But when you allocate D, the C subobject refers to the instance of A that came with B, so it's offset is different.

The obvious answer is: because the standard says so. The motivation behind this in the standard is that static_cast should be close to trivial—at most, a simple addition or subtraction of a constant to the pointer. Where s the downcast to a virtual base would require more complicated code: perhaps even with an additional entry in the vtable somewhere. (It requires something more than constants, since the position of D relative to A may change if there is further derivation.) The conversion is obviously doable, since when you call a virtual function on an A*, and the function is implemented in D, the compiler must do it, but the additional overhead was considered inappropriate for static_cast. (Presumably, the only reason for using static_cast in such cases is optimization, since dynamic_cast is normally the preferred solution. So when static_cast is likely to be as expensive as dynamic_cast anyway, why support it.)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!