我不明白那里发生了什么。使用Visual Studio 2008,我定义了这样的枚举:
enum MyType{
A,
B,
C
};
然后,我使用它来初始化成员变量:
class MyClass
{
private:
bool bit_;
uint16 num_;
MyType member_;
public:
MyClass(MyType value){
member_ = value; // This the assignment which fails
} // Here, there's a breakpoint to query member_ value
};
myClass实例= new myClass();
我正在使用调试配置,因此在阅读变量时,没有任何优化可以欺骗我。在分配后的断点,调试器将Member_的值显示为 member_ = C
.
它可能是一个调试器Whatch Refresh问题,但是,在检查时评估正确的方法中:
if(member_ == C){
// ??
}
而且,分配给其他值会产生奇怪的数字,例如执行 member_ = B
给予 member_ = 258
当它ferch。你能告诉我怎么了吗?提前致谢。
编辑#1
我注意到一个有趣的效果,解释了为什么分配后 member_ = A
表达方式 member_ == C
评估 true
对于具有默认值的枚举:
对于枚举
enum MyType{ A, B, C}; // equivalent to enum MyType{ A = 0, B = 1, C = 2};
我明白了
MyType a = A; // but when fetching a the value is 2 (0x0002) thats the map for C!
MyType b = B; // but when fetching a the value is 258 (0x0102)
MyType c = C; // but when fetching a the value is 514 (0x0202)
但是如果我做
enum MyType{ A = 5, B = 6, C = 7};
我明白了
MyType a = A; // but when fetching a the value is 1282 (0x0502)
MyType b = B; // but when fetching a the value is 1538 (0x0602)
MyType c = C; // but when fetching a the value is 1794 (0x0702)
因此,在分配#?!^%枚举时,该规则似乎是Shift 8位并添加2。这听起来像编译器问题。
顺便说一句 member_
成为 int
反而 MyType
什么都没有改变。
编辑#2在课堂上增加了两个成员,这是问题的真正原因。时间限制消失后,我将立即发布答案(问题的发布为8h)。