Array of structs - initialization errors

前端 未结 2 1331
臣服心动
臣服心动 2021-01-19 07:16

I\'m creating some data structures here (with MFC), compiling in MS Visual C++ 6.0 (yes, it\'s old).

struct SOpcodeDa         


        
相关标签:
2条回答
  • 2021-01-19 07:52

    We've figured out another solution, without using #define: using a constructor to initialize the data structures. Sorta like this:

    struct SOpcodeData
    {
        SOpcodeData (const BYTE byDataType, const DWORD dwMinValue, const DWORD dwMaxValue, const WORD wRepeat)
          : m_byDataType(byDataType), m_dwMinValue(dwMinValue), m_dwMaxValue(dwMaxValue), m_wRepeat(wRepeat)
        {}
    
        BYTE m_byDataType;
        DWORD m_dwMinValue;
        DWORD m_dwMaxValue;
        WORD m_wRepeat;
    };
    
    SOpcodeData MY_BYTE  (DATA_U8,   0,  UCHAR_MAX,  1);
    SOpcodeData MY_WORD  (DATA_U16,  0,  USHRT_MAX,  1);
    SOpcodeData MY_DWORD (DATA_U32,  0,  UINT_MAX,   1);
    
    SOpcodeData foo[] = { MY_BYTE, MY_WORD, MY_DWORD };
    

    Thanks to everyone!

    0 讨论(0)
  • 2021-01-19 07:58

    Since you insist on using a compiler with lots and lots of known bugs, you will need an ugly workaround:

    #define MY_BYTE_CONTENT  { DATA_U8,   0,  UCHAR_MAX,  1 }
    #define MY_WORD_CONTENT  { DATA_U16,  0,  USHRT_MAX,  1 }
    #define MY_DWORD_CONTENT { DATA_U32,  0,  UINT_MAX,   1 }
    
    SOpcodeData MY_BYTE  = MY_BYTE_CONTENT;
    SOpcodeData MY_WORD  = MY_WORD_CONTENT;
    SOpcodeData MY_DWORD = MY_DWORD_CONTENT;
    
    SOpcodeData foo[] = { MY_BYTE_CONTENT, MY_BYTE_CONTENT, MY_WORD_CONTENT, MY_DWORD_CONTENT, MY_BYTE_CONTENT };
    
    0 讨论(0)
提交回复
热议问题