【发布时间】:2012-08-27 01:14:34
【问题描述】:
为什么我不能使用 reinterpret_cast 操作符进行这样的转换?
enum Foo { bar, baz };
void foo(Foo)
{
}
int main()
{
// foo(0); // error: invalid conversion from 'int' to 'Foo'
// foo(reinterpret_cast<Foo>(0)); // error: invalid cast from type 'int' to type 'Foo'
foo(static_cast<Foo>(0));
foo((Foo)0);
}
【问题讨论】:
-
static_cast是这里的正确操作。 -
为什么有效?你认为 reinterpret_cast 是干什么用的?
-
我认为 reinterpret_cast 可以用于所有类型的转换,因为它会强制将任何类型转换为另一种类型,并具有此转换的所有副作用。
标签: c++ enums static-cast