结构中的默认值(char)

Luk*_*sas 1 c++ struct char assign

我想请求帮助.当我得到这个:

struct MyStruct
{
    unsigned char myBytes[5];
    MyStruct()
    {
        myBytes[0] = 0x89;
        myBytes[1] =  0x50;
        myBytes[2] =  0x4E;
        myBytes[3] =  0x47;
        myBytes[4] =  0x0D;
    }        
};
Run Code Online (Sandbox Code Playgroud)

如何让它变得简单?比如myBytes = {0x89,0x50,0x4E,0x47,0x0D};

Jos*_*eld 5

在C++ 11中,您可以执行以下任一操作:

struct MyStruct
{
    unsigned char myBytes[5] = {0x89, 0x50, 0x4E, 0x47, 0x0D};     
};

// or...

struct MyStruct
{
    unsigned char myBytes[5];
    MyStruct() : myBytes{0x89, 0x50, 0x4E, 0x47, 0x0D}
    { }        
};
Run Code Online (Sandbox Code Playgroud)

否则,你已经拥有了最好的方法.