I'm having trouble knowing what I'm doing wrong.
Huge& Huge::operator *=(const Huge& h) {
Huge retHuge = *this * h;
string out1 = retHuge.toString(); // for debugging purposes
return *this = retHuge;
// return retHuge;
}
The
Huge retHuge = *this * h;
works fine as I verify in the string out1
. If I return retHuge and print the result in the calling routine, I can see that the original this remains unchanged. If I do a *this = retHuge
and return *this
, I get a SEGSEGV
fault.
What is the proper way to call the destructor to clean up memory and to return the result after the multiply?
I would like to thank all of you for the answers. Crazy Eddie used what I had and extended it, which is exactly what I wanted to do. I changed the source to reflect his suggestion and the SEGSEGV fault comes up.
I take this to mean that my syntax must be basically correct but that I have a bug hidden in the rest of my code. This is valuable information and I need to go back and very carefully look to see if I can find something. I don't want to be lazy and ask for help before I do my homework properly, so I'll have a good look first.
My experience is mainly with Java which has a nice garbage collector for unreferenced objects. I'll go through with the debugger and verify that my destructor is being called to free the memory.
That was a good tip to try to do the *= operator first and then build the = operator from it. Clearly I did things in the opposite direction. Still your answers seem to indicate that what I did should indeed work. Since it doesn't work, I'll go back and see if I can find anything. If, after I have done my homework, I still can't find anything, I'll continue to ask. In the meantime, thanks for all the help.