gpt4 book ai didi

c++ - 被类 vector 混淆 : "The Big Three", 在 push_back 和资源管理之后删除一个类

转载 作者:行者123 更新时间:2023-11-28 01:26:27 25 4
gpt4 key购买 nike

我有这样一个类:

class cSerialMessage
{
public:
cSerialMessage(const enumMessages type, std::string txmessage = "") {rxAnswer="";};

// Get/Set Methods
// ...
bool AddIntParam(); // Stores an int as parameter for the message
void BuildCompleteMessage() // Build the whole message following the protocol rules

private:
enumMessages m_type;
std::string m_txmessage, m_rxanswer;
// [...]
};

然后我有另一个“管理”消息队列的类:

class cMsgManager
{
public:
cMsgManager();

// [...]
cSerialMessage* NewMessage(eMessages MSG);
bool AddParameter(int Param);
bool QueueMessage(bool FirstPosition);

private:
deque<cSerialMessage> cMS;
cSerialMessage* tempMsg;
}

相关部分是创建 cSerialMessage 并将其添加到双端队列的地方。

cSerialMessage* cMsgManager::NewMessage (eMessages MSG)
{
// Begin the creation of a new message


if (tempMsg!=nullptr) {
// there was already a temporary message
OutputDebugString ("NOPE!");
return nullptr;
}
tempMsg = new cSerialMessage(MSG);
return tempMsg;
}
//------------------------------------------------------------------------------
bool cMsgManager::AddParameter (int Param)
{
if (tempMsg==nullptr) {
// this means that NewMessage() was'nt called before.
OutputDebugString ("NOPE!);
return false;
}
return tempMsg->AddIntParam(Param);
}
//------------------------------------------------------------------------------
bool cMsgManager::QueueMessage(bool FirstPosition)
{
if (tempMsg==nullptr) {
// this means that NewMessage() was'nt called before.
OutputDebugString ("NOPE!);
return false;
}

// Build the final message
tempMsg->BuildCompleteMessage();

if (FirstPosition) {
cMS.push_front(*tempMsg);
}else{
cMS.push_back(*tempMsg);
}

delete tempMsg;

tempMsg=nullptr;
return true;
}

尽管关于这个主题的所有问题 ( this is very detailed ),我仍然感到困惑。

我应该删除我的 tempMsg 吗?它是在双端队列中复制的,还是最终由 tempMsg 指向的数据是稍后从双端队列访问的数据?还是我必须创建复制构造函数和复制赋值运算符?

最佳答案

  1. Should i delete my tempMsg?

是的,如所写,您必须删除您的tempMsg,或者在程序的生命周期中重复使用单个实例(不要制作 第一个之后的)。重用它可能需要在每次重用之前将其清除,这似乎不值得。

  1. Is it copyed in the deque or, in the end, the data pointed by tempMsg are the one that will be later accessed from the deque?

已复制; push_back 接收引用,但仅记录为复制或移动(并且由于您传递了一个左值,它将进行复制)。您可以通过执行 cMS.push_front(std::move(*tempMsg)); 来清空 tempMsg(因为您只是要之后将其删除,因此您也可以保存拷贝)。

  1. Or have i to create the copy-constructor and copy-assignement operator?

假设您的 cSerialMessage 的所有成员本身都是可正确复制的(没有原始指针等),并且您没有定义任何自定义复制/移动操作或析构函数,您应该没问题;编译器生成的复制构造函数可以正常工作。另一方面,cMsgManager 需要完整的 3/5 构造函数和析构函数规则,因为您没有为 tempMsg 使用智能指针或值语义。

请注意,整个动态分配的事情毫无意义/浪费。您可以将 tempMsg 设为 cSerialMessage(而不是 cSerialMessage*),然后按值使用它。您可以将它保留为实例的属性,或者只让 NewMessage 返回实际的新消息,根本不在本地存储拷贝。本地拷贝将使线程或可重入代码成为噩梦,因此按值返回新消息并让调用者管理它可能是一个更好的主意。

关于c++ - 被类 vector 混淆 : "The Big Three", 在 push_back 和资源管理之后删除一个类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53558935/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com