c – 为什么在这里使用static_cast而不是reinterpret_cast很重要
At a reply of a blog post of Raymond Chen,
提问者指出
雷蒙德回答
看完他的意见后,我可以猜到.在示例中使用static_cast很好,但reinterpret_cast不是.因为reinterpret_cast不转换vtable.我明白了吗? 我重读了更有效的C的演员说明,以了解这一点.但是没有答案. 解决方法
不,问题是reinterpret_cast完全忘记了遗产.它将简单地返回相同的地址not1.但static_cast知道你正在执行一个downcast:即从基类转换为派生类.既然知道这两种类型,所以相应地调整地址,即做正确的事情. 假设我们的实现规定了假设的OVERLAPPEDEX类,它具有如下虚拟函数: +------+------------+------------------+-------------+ | vptr | OVERLAPPED | AssociatedClient | ClientState | +------+------------+------------------+-------------+ ^ | ptr 我们给出的指针指向OVERLAPPED子对象. reinterpret_cast不会改变.它只会改变类型.显然,通过这个地址访问OVERLAPPEDEX类很容易造成破坏,因为它的子对象的位置现在都是错误的! what we believe we have when we access OVERLAPPEDEX through the pointer +------+------------+------------------+-------------+ | vptr | OVERLAPPED | AssociatedClient | ClientState | +------+------+-----+------+-----------+------+------+------+ | vptr | OVERLAPPED | AssociatedClient | ClientState | <- what we actually have +------+------------+------------------+-------------+ ^ | ptr static_cast知道要将OVERLAPPED *转换为OVERLAPPEDEX *,它必须调整地址,并做正确的事情: +------+------------+------------------+-------------+ | vptr | OVERLAPPED | AssociatedClient | ClientState | +------+------------+------------------+-------------+ ^ | ptr after static_cast
C风格的演员被定义为成功的第一个: > const_cast 正如你所看到的,static_cast在reinterpret_cast之前尝试过,所以在这种情况下,C风格的演员也会做正确的事情. More info 没有保证.对于reinterpret_cast会发生什么,几乎没有什么保证.我知道的所有实现将简单地发出相同的地址不变. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |