My question is about a block of code I saw in particular C++ book. The following is the code:
string& string:perator = (const string& Str)
{
char* temp = new char[Str.m_Length];
m_Length = Str.m_Length;
memcpy(temp,Str.m_Data,m_Length);
delete [ ] m_Data;
m_Data = temp;
return *this;
}
This is a constructor for a home made string class. I understand most of the code in this block but I had a question as to why the author would use the delete operator on m_data and then immediatly after deleting it use it to hold something else. Wouldn't you have to issue another New statement in order to use m_data again? I would think if you didn't use New again that you couldn't be sure m_data would always but to a valid address.
As you can probably tell I am fairly new to C++, although I have been programming for a long while in other languages. I know this isn't really a "problem" so much as it is just a question I had to help my understanding of C++. Any help would be greatly appreciated.
string& string:perator = (const string& Str)
{
char* temp = new char[Str.m_Length];
m_Length = Str.m_Length;
memcpy(temp,Str.m_Data,m_Length);
delete [ ] m_Data;
m_Data = temp;
return *this;
}
This is a constructor for a home made string class. I understand most of the code in this block but I had a question as to why the author would use the delete operator on m_data and then immediatly after deleting it use it to hold something else. Wouldn't you have to issue another New statement in order to use m_data again? I would think if you didn't use New again that you couldn't be sure m_data would always but to a valid address.
As you can probably tell I am fairly new to C++, although I have been programming for a long while in other languages. I know this isn't really a "problem" so much as it is just a question I had to help my understanding of C++. Any help would be greatly appreciated.