Temporary object debacle
I have an overloaded ~ operator...which returns a temp object...which I can't get around because that's the way my professor would like. I have an assignment operator for objects as well....basically I have the call..... t21 = ~s21. But when t21 goes to assign s21 to it, there's nothing in the member buf because it got ran through the deconstructor.... (I think)... So I'm at a wall with my limited knowledge. Any thoughts....(besides not returning temp objects)...
This will be one of my last posts as I'm graduating in three weeks....and I still don't know anything...LOL!!!!
int size = m_length;
char* temp = new char[size];
for(i = m_length - 1, j = 0; i >= 0; i--, j++)
temp[j] = m_buf[i];
temp[size] = '\0';
ReverseString& ReverseString::operator=(const ReverseString& rhs)
if(this == &rhs)
m_length = rhs.m_length;
if(NULL != m_buf)
m_buf = NULL;
m_buf = new char[m_length + 1];
strcpy_s(m_buf, m_length + 1, rhs.m_buf);
As a rough rule, if you have to hand-roll an assignment operator (as you have above) you also need to hand-roll a copy constructor. Among other things, that is necessary to ensure that operations involving copying to and from temporaries can work correctly.
>> temp[size] = '\0';
Also, note that this line accesses memory beyond the bounds of your array. Notice how you add one to the size when allocating in the other function you posted. You should probably do the same in the ReverseString function.
> temp[size] = '\0';
This steps off the end of your array.
Also, you don't have a delete [ ] temp;