What does the C ++ union do in this case?

In one of the classes I'm working on, I found something like this in the header file:

// Flags
union
{
    DWORD               _flags;
    struct {
        unsigned        _fVar1:1;
        unsigned        _fVar2:1; 
        unsigned        _fVar3:1; 
        unsigned        _fVar4:1;  
    };
};

In some elements of the class, I saw that it _flagsinstalled just like _flags = 3;. I also saw that members in the structure are set directly, for example, _fVar1 = 0and compared.

I'm trying to remove _fVar1, I'm not sure what he would do in other places will be available or installed _flagsand others _fVar#. For example, setting _flags = 3means _fVar1and _fVar2will be 1 and _fVar3, as _fVar4will be 0? Would you delete or add to the structure that I should make appropriate changes to the codes that apply to any of the other members in the association?

+4
2

- () ++, .

undefined.

, : , .


C ( C11, C99) , ++ ( C). , .

, _flags = 3 , _fVar 1 _fVar2 1 _fVar3, _fVar4 0?

, , . , .

, , , 0 (_flags = 0) 1 (_flags = -1).

struct , , ?

, , .

+6

. .

, .

+1

Source: https://habr.com/ru/post/1666752/


All Articles