使用UNION,此示例代码的目的是什么?

时间:2016-09-22 09:04:58

标签: c unions

有一些示例代码aboudt设计模式,我不知道为什么第一个变量" IQRCodeState"已在UNION中定义。任何人都可以帮我解释一下UNION的具体用法吗?

union
{
    IQRCodeState;
    IQRCodeState iqrcodeState;
};

更多示例代码如下:

typedef struct _IQRCodeState IQRCodeState;

struct _IQRCodeState
{
    void (*getQRCode)(IQRCodeState*);
    void (*scanQRCode)(IQRCodeState*);
    void (*auth)(IQRCodeState*);
    void (*cancel)(IQRCodeState*);
    void (*spend)(IQRCodeState*);
};  

typedef struct _QRCodeStateMachine QRCodeStateMachine;

enum _QRCodeState
{
    NOQRCODE,
    VALID,
    AUTHORIZING,
    AUTHORIZED
};

struct _QRCodeStateMachine
{
    IQRCodeState* currentState;
    IQRCodeState* noQRCodeState;
    IQRCodeState* qrcodeValidState;
    IQRCodeState* qrcodeAuthorizingState;
    IQRCodeState* qrcodeAuthorizedState;
    void (*setState)(QRCodeStateMachine*, enum _QRCodeState);
    union
    {
        IQRCodeState;
        IQRCodeState iqrcodeState;
    };
};

stateMachine->getQRCode(&stateMachine->iqrcodeState);
stateMachine->scanQRCode(&stateMachine->iqrcodeState);
stateMachine->auth(&stateMachine->iqrcodeState);
stateMachine->spend(&stateMachine->iqrcodeState);

0 个答案:

没有答案