我需要将包含地址的整数类型转换为实际的指针类型。我可以使用 reinterpret_cast 如下:
MyClass *mc1 = reinterpret_cast<MyClass*>(the_integer);
但是,这不会执行任何运行时检查以查看所讨论的地址是否实际上包含 MyClass 对象。我想知道首先转换为 void*(使用 reinterpret_cast)然后在结果上使用 dynamic_cast 是否有任何好处。像这样:
void *p = reinterpret_cast<void*>(the_integer);
MyClass *mc1 = dynamic_cast<MyClass*>(p);
assert(mc1 != NULL);
使用第二种方法有什么好处吗?