是否有使用static_cast< int>每时每刻? [英] Is there an alternative to using static_cast<int> all the time?
问题描述
我有一个名为 StackID
的枚举,在我的代码中,我必须 static_cast
c> int 相当多 - 例如
I have an enumeration called StackID
, and throughout my code I have to static_cast
it to int
quite a bit - e.g.
StackID somestack;
int id = static_cast<int>(somestack);
有一个简单的替代方法吗?我听说过隐性转换 - 这是我可以在这里使用吗?
Is there a shorthand alternative to doing this cast over and over again ? I have heard of "implicit" conversions - is that something I can use here?
(可能与此问题)
推荐答案
是否有应该使用的东西?可能不会。如果你正在做枚举转换为int我会问,如果你正确使用枚举(或者如果你必须与一个遗留的API接口)。据说你不 static_cast枚举到ints。
Is there something you should use instead? Probably not. If you're doing enum casts to int I would question if you're using enums properly (or if you're having to interface with a legacy API.) That being said you don't have to static_cast enums to ints. That'll happen naturally.
查看这篇文章从MSN on枚举和enum-> int和int->枚举(你必须使用static_cast。)
See this article from MSN on enums and enum->int and int->enum (where you do have to use a static_cast.)
这篇关于是否有使用static_cast< int>每时每刻?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!