Article View: uw.dcs.general
Article #2Re: Operator Overloading problem
From: Paul Johns
Date: Thu, 17 Dec 1992 21:40
Date: Thu, 17 Dec 1992 21:40
33 lines
1485 bytes
1485 bytes
Any time you have a class which contains a pointer to dynamically-allocated memory (as your class does), you WILL NEED an appropriate: Assignment operator Copy constructor Destructor You will likely also need a default constructor. Your symptoms are consistent with forgetting one or both of the assignment operator or copy constructor, depending on how the addition operator was used. (Forgetting the destructor results in a memory leak.) If you forget any of these, you'll end up with the kinds of problems you're seeing. Note that these problems can be silent: since the compiler provides you with these functions if you don't provide them, you won't get any sort of compiler error. What I think is happening in your case is that the compiler supplied assignment operator or copy constructor is just doing a memberwise copy of your object. Your addition function is creating the temporary and allocating memory for it. The pointer to this memory is copied from the temporary to the object to which you're assigning/copying rather than copying the memory. // Paul Johns
Technical Details
Message-ID:
<1992Dec17.214050.24924@microsoft.com>Path:
arc1-ca.pugleaf.net!sparky!uunet!microsoft!wingnut!pauljoReferences:
<1992Dec16.192756.13271@dcs.warwick.ac.uk>Show Full Headers
Complete Article Headers
Newsgroups: uw.dcs.general,comp.lang.c++ Path: arc1-ca.pugleaf.net!sparky!uunet!microsoft!wingnut!pauljo From: pauljo@microsoft.com (Paul Johns) Subject: Re: Operator Overloading problem Message-ID: <1992Dec17.214050.24924@microsoft.com> Date: 17 Dec 92 21:40:50 GMT Organization: Microsoft, Redmond WA USA References: <1992Dec16.192756.13271@dcs.warwick.ac.uk> Lines: 34