简体   繁体   English

如何在C ++中创建类型安全的int-枚举?

[英]How to create type-safe int - enum in C++?

I need to create many classes that are somewhere between integer and enum. 我需要创建许多介于整数和枚举之间的类。 Ie have the arithmetics of integer but also are not implicitly converted to int. 即具有整数算法,但也不会隐式转换为int。

Have a look at the answer to this question - BOOST_STRONG_TYPEDEF did exactly what I wanted. 看看这个问题的答案-BOOST_STRONG_TYPEDEF正是我想要的。

// macro used to implement a strong typedef.  strong typedef
// guarentees that two types are distinguised even though the
// share the same underlying implementation.  typedef does not create
// a new type.  BOOST_STRONG_TYPEDEF(T, D) creates a new type named D
// that operates as a type T.

一种可能性是使用:: GetValue()方法创建一个类,该类的“枚举”定义为该类的不变成员,并且您需要使用重载的数学运算才能使用GetValue()进行相关的数学运算。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM