2
我需要创建许多在整数和枚举之间的类。 也就是说有整数的算术,但也不会隐式转换为int。如何在C++中创建类型安全的int - 枚举?
我需要创建许多在整数和枚举之间的类。 也就是说有整数的算术,但也不会隐式转换为int。如何在C++中创建类型安全的int - 枚举?
看看this question的答案 - 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()来执行数学运算题。