[英]should I use it or static_cast<void*> then static_cast<myType*> to avoid reinterpret_cast?
[英]Why does Qt use reinterpret_cast rather than static_cast for void*?
您可以使用static_cast向/从任何指向T的指向/从void *的任何指针进行转换,为什么Qt使用reinterpret_cast?
int SOME_OBJECT::qt_metacall(QMetaObject::Call _c, int _id, void **_a)
{
_id = QMainWindow::qt_metacall(_c, _id, _a);
if (_id < 0)
return _id;
if (_c == QMetaObject::InvokeMetaMethod) {
switch (_id) {
// Why reinterpret_cast here??
case 0: on_tabWidget_tabCloseRequested((*reinterpret_cast< int(*)>(_a[1]))); break;
default: ;
}
_id -= 1;
}
return _id;
}
坦率地说,我也从未弄清楚。 void **
结构以相同的方式创建,只需将int*
转换为void*
,然后在另一侧执行此奇怪的转换。 据我所知,static_cast不仅好,而且会更好。
您会发现Qt等大型项目中存在大量可疑代码。 有时东西会通过评论或只是坚持下去,因为没有人想要经历改变它的麻烦。
这有点旧,但我不同意共识。 您不应该使用static_cast
从任何类型转换为void*
,只能使用reinterpret_cast
执行此操作。 static_cast
保留用于所谓兼容的类型,执行一些编译时检查(即派生类,它们之间的数字类型)。
从这个链接MSDN :
dynamic_cast Used for conversion of polymorphic types.
static_cast Used for conversion of nonpolymorphic types.
const_cast Used to remove the const, volatile, and __unaligned attributes.
reinterpret_cast Used for simple reinterpretation of bits.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.