简体   繁体   English

为什么我不能为 c++ 枚举类型变量分配正确的值?

[英]Why I can't assign the correct value to c++ enum type variable?

I can't understand what's going there.我无法理解那里发生了什么。 Using Visual Studio 2008, I've defined an enum like this:使用 Visual Studio 2008,我定义了一个像这样的枚举:

enum MyType{
    A,
    B,
    C
};

Then I use it to initialize a member variable:然后我用它来初始化一个成员变量:

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 instance = new MyClass(); MyClass 实例 = new MyClass();

I'm using Debug configuration, so no optimization can fool me when reading a variable.我正在使用调试配置,因此在读取变量时没有任何优化可以欺骗我。 At a breakpoint just after the assignment, the debuger shows the value of member_ as member_ = C .在赋值之后的断点处,调试器将member_ = C的值显示为member_ = C

It could be a debugger whatch refresh issue but, inside a method it evaluates true when checking:这可能是调试器的什么刷新问题,但是,在检查时它评估为真的方法中:

if(member_ == C){
    // ??
}

And also, assigning to the other values gives strange numbers, eg doing member_ = B gives member_ = 258 when ferching it.而且,分配给其他值会产生奇怪的数字,例如,在获取它时,执行member_ = B会得到member_ = 258 Can you tell me what's wrong?你能告诉我有什么问题吗? Thanks in advance.提前致谢。

EDIT #1编辑#1

I've noted a funny effect which explains why after assigning member_ = A the expression member_ == C evaluates to true for the enum with the default values:我注意到一个有趣的效果,它解释了为什么在分配member_ = A ,表达式member_ == C对于具有默认值的枚举计算为true

For the enum对于枚举

enum MyType{ A, B, C}; // equivalent to enum MyType{ A = 0, B = 1, C = 2};

I get我得到

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)

But if I make但如果我让

enum MyType{ A = 5, B = 6, C = 7};

I get我得到

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)

So, when assigning that #?!^% enum, the rule seems to be, shift 8 bits and add 2. It sounds like compiler issues.因此,在分配 #?!^% 枚举时,规则似乎是,移位 8 位并添加 2。这听起来像是编译器问题。

Btw, making the type of member_ to be int instead MyType doesn't changes nothing.顺便说一句,将member_的类型设为int而不是MyType不会改变任何内容。

EDIT #2 Added two more members to the class, which are the real cause of the problem.编辑 #2向类中添加了另外两个成员,这是问题的真正原因。 I'll post the answer as soon as the time restriction vanishes (8h from the posting of the question).我会在时间限制消失后立即发布答案(发布问题后 8 小时)。

Enum entries does not have to have unique values.枚举条目不必具有唯一值。 You might have an enum with A,B,C where both A and C are equal to '42'.您可能有一个包含 A、B、C 的枚举,其中 A 和 C 都等于“42”。 In such case, when the var has value of 42, the IDE will show you only one of the matching entries, A or C, not both.在这种情况下,当 var 的值为 42 时,IDE将仅显示匹配条目之一,A 或 C,而不是两者。

You can easily create an enum with 'duplicate' entries, like this:您可以轻松地创建一个带有“重复”条目的枚举,如下所示:

enum { A=1, B=1, C=1 }

But most probably, you didn't do it like that.但很可能,你没有那样做。 However, when using autonumbering, you can accidentally (or intentionally) create duplicates too:但是,在使用自动编号时,您也可能会意外(或有意)创建重复项:

enum { A, B, C }       // 0,1,2
enum { A, B, C=0 }     // 0,1,0
enum { A=2, B=1, C }   // 2,1,2
enum { A=-1, B, C=0 }  // -1,0,0

Be sure to double-check your enum definition.请务必仔细检查您的枚举定义。

see ie http://www.learncpp.com/cpp-tutorial/45-enumerated-types/见即http://www.learncpp.com/cpp-tutorial/45-enumerated-types/

Thanks everyone.谢谢大家。 Somebody at work pointed me to the origin of the problem.工作中的某个人向我指出了问题的根源。

It's a matter of faulty configuration of the project, which gives data alignment issues.这是项目配置错误的问题,这会导致数据对齐问题。

After compiling, with the current project settings, the two first members of the class are 3 bytes of size.编译后,根据当前项目设置,类的前两个成员大小为 3 个字节。

It should be 4 bytes, hence the misalignment of 1 byte.它应该是 4 个字节,因此 1 个字节未对齐。 The extra 2 added is garbage at the misaligned byte, so, the whole effect is to shift one byte and add 2.添加的额外 2 是未对齐字节处的垃圾,因此,整个效果是移动一个字节并添加 2。

This effect is cancealed adding an extra member, although it's not an elegant solution (the solution is to configure the project corectly).这种效果被取消添加额外的成员,虽然这不是一个优雅的解决方案(解决方案是正确配置项目)。

class MyClass
{
    private:
        bool   bit_;  // 1 byte
        uint16 num_;  // 2 byte

        bool dummy;   // extra byte to help the alignment

        MyType member_;
    public:
        MyClass(MyType value){
            member_ = value; // Now it works as expected.
        }
};

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

相关问题 如何在 C++ 中将状态输入到枚举类型的变量中? - How can I input a state to a variable of enum type in C++? 如何将枚举值分配给用户定义的双精度变量? C ++ - How to assign an enum value to a double variable defined by the user ?? C++ 我可以使用类型特征从c ++中的枚举值推断出枚举的类型吗? - Can I use type traits to deduce the type of an enum from an enum value in c++? c++:为什么我不能为类“内部”的非常量静态成员赋值? - c++ : why I can't assign a value to a non-const static member "inside" a class? 为什么我不能在 C++ 中为整数指针分配一个整数值? - Why can't I assign an integer value to an integer pointer in C++? 为什么我可以在C ++中将现有引用分配给文字值? - Why can I assign an existing reference to a literal value in C++? 为什么我不能在C ++中将一个迭代器分配给另一个迭代器? - Why can't I assign one iterator to another in C++? C ++中new运算符的返回类型是什么?为什么我们可以将返回值分配给变量(非指针)? - what is the return type of the new operator in c++ & why can we assign the returned value to a variable(non-pointer)? 我无法在列表 c++ 中为 object 赋值 - I can't assign value to object in list c++ 为什么不能为命名空间中的变量分配值? - Why can't I assign a value to a variable in a namespace?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM