Question

I can't understand what's going there. Using Visual Studio 2008, I've defined an enum like this:

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();

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.

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, e.g. doing member_ = B gives member_ = 258 when ferching it. Can you tell me what's wrong? Thanks in advance.

EDIT #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:

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.

Btw, making the type of member_ to be int instead MyType doesn't changes nothing.

EDIT #2 Added two more members to the class, which are the real cause of the problem. I'll post the answer as soon as the time restriction vanishes (8h from the posting of the question).

Was it helpful?

Solution 2

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.

It should be 4 bytes, hence the misalignment of 1 byte. The extra 2 added is garbage at the misaligned byte, so, the whole effect is to shift one byte and add 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.
        }
};

OTHER TIPS

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'. 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.

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 i.e. http://www.learncpp.com/cpp-tutorial/45-enumerated-types/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top