operator overloading/destructor problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • schmals
    New Member
    • Oct 2007
    • 8

    #1

    operator overloading/destructor problem

    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
    New Member
    • May 2007
    • 104

    #2
    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....

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      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.

      Comment

      • schmals
        New Member
        • Oct 2007
        • 8

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

        Comment

        Working...