简体   繁体   English

在结构内部初始化向量

[英]Initialize a vector inside a struct

I have the following struct: 我有以下结构:

struct MsgProperties
{
    DWORD               msgSize;
    std::vector<BYTE>   vbuffer;

    //-Constructor
    MsgProperties(DWORD A = 0) : msgSize(A){}
};

I want to use that struct with a c++ vector so this is what I've done: 我想将该结构与C ++向量一起使用,所以这就是我所做的:

std::vector<MsgProperties> ReadText;
BYTE buffer[MAX_BUFFER_SIZE];
DWORD bytesRead;
do
{
    bytesRead = myFile.Read(buffer, MAX_BUFFER_SIZE);
    ReadText.push_back(MsgProperties(bytesRead, std::vector<BYTE>((BYTE*)buffer, (BYTE*)buffer + bytesRead)));

} while (bytesRead > 0);

but I can't figure out how to make it work correctly. 但我不知道如何使其正常工作。 Can someone tell me what I am missing? 有人可以告诉我我想念什么吗?

Looks like you need another 2 constructors: 看来您还需要2个构造函数:

MsgProperties(DWORD A, const std::vector<BYTE>& vec) : msgSize(A), vbuffer(vec) {}
MsgProperties(DWORD A, std::vector<BYTE>&& vec) : msgSize(A), vbuffer(vec) {}

Alernatively, a single constructor would be good too: 替代地,单个构造函数也将是不错的:

MsgProperties(DWORD A, std::vector<BYTE> vec) : msgSize(A), vbuffer(std::move(vec)) {}

On a side note, I do not see why you need message size at all. 顺便说一句,我根本看不到为什么您需要消息大小。 the size of the vector is the message size. 向量的大小就是消息的大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM