Connecting Tech Pros Worldwide Forums | Help | Site Map

operator overloading/destructor problem

schmals's Avatar
Newbie
 
Join Date: Oct 2007
Location: Washington
Posts: 8
#1: Oct 31 '07
I am building a bigint class and currently having an issue with overloading operators simple +, -, and * operators with my destructor. The bigint class contains a dynamically allocated array of int's. When my destructor is called, the dynamically allocated memory gets dissolved.

When overloading +, -, and *, I create a new bigint called answer which, obviously, holds the answer to the operations. My problem is that I return answer from the operator overloading functions, and at that point my destructor is automatically called and deletes the array I was trying to pass back as a part of the answer. This is causing me all sorts of trouble. Any ideas on how to fix it??

Thank You!!!!!

arunmib's Avatar
Member
 
Join Date: May 2007
Posts: 104
#2: Oct 31 '07

re: operator overloading/destructor problem


I think you will know that "a destructor is called for a class object when that object passes out of scope" . You are certain this is not your case.....This is a simple thing, but many of us (rather me) tend to forget this often....
Moderator
 
Join Date: Mar 2007
Location: North Bend Washington USA
Posts: 5,366
#3: Oct 31 '07

re: operator overloading/destructor problem


Quote:

Originally Posted by schmals

My problem is that I return answer from the operator overloading functions, and at that point my destructor is automatically called and deletes the array I was trying to pass back as a part of the answer.

I'll bet you haven't written a copy constructor and are just passing back the address of the array rather than a copy of it.
schmals's Avatar
Newbie
 
Join Date: Oct 2007
Location: Washington
Posts: 8
#4: Nov 1 '07

re: operator overloading/destructor problem


ah, yes... you are completely right... i did miss that... thank you much!
Reply