我想寻求帮助。 当我得到这个时:
struct MyStruct
{
unsigned char myBytes[5];
MyStruct()
{
myBytes[0] = 0x89;
myBytes[1] = 0x50;
myBytes[2] = 0x4E;
myBytes[3] = 0x47;
myBytes[4] = 0x0D;
}
};
如何让它变得简单? 喜欢 myBytes = {0x89, 0x50, 0x4E, 0x47, 0x0D};
最佳答案
在 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}
{ }
};
否则,您已经找到了最好的方法。
关于c++ - 结构中的默认值(字符),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20838751/