【发布时间】:2014-12-26 07:35:42
【问题描述】:
关于这个问题:When to use reinterpret_cast?
我发现了某事。像这样:
template<typename T> bool addModuleFactoryToViewingFactory(ViewingPackage::ViewingFactory* pViewingFactory)
{
static_cast<ModuleFactory*>(reinterpret_cast<T*>(0)); // Inheritance compile time check
...
}
这是在编译时检查 T 是否可以转换为 ModuleFactory 的好方法吗?
我的意思是,检查程序员是否将有效的东西放入<>of addModuleFactoryToViewingFactory<T>(...)
这是好的、好的还是唯一的方法?
问候
【问题讨论】:
-
是的。它没有错……但这是一个好方法还是唯一的方法?
-
这可能不太好,记住 static_cast 双向有效,reinterpret_cast 在这里完全没有根据(你需要另一个 static_cast 代替)。
标签: c++ casting reinterpret-cast