I have a class like this:
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;
// [...]
};
Then i have another class which "manages" a queue of messages:
class cMsgManager
{
public:
cMsgManager();
// [...]
cSerialMessage* NewMessage(eMessages MSG);
bool AddParameter(int Param);
bool QueueMessage(bool FirstPosition);
private:
deque<cSerialMessage> cMS;
cSerialMessage* tempMsg;
}
The relevant part is where the cSerialMessage are created and added to the deque.
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;
}
Despite all the questions about this topic ( this is very detailed), i'm still confused.
Should i delete my tempMsg? 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? Or have i to create the copy-constructor and copy-assignement operator?