Home › Forums › C Programming › Question on operator overloading… and memory management
- This topic has 1 reply, 1 voice, and was last updated 15 years, 7 months ago by AbbeyCarpenter.
- AuthorPosts
- March 5, 2009 at 7:00 pm #2179AbbeyCarpenterParticipant
Maybe not directly on operator overloading… but associated with it – kind of… it’s more about memory management. lemme ‘splain!
I’m going back through my SAMS teach yourself C++ in 24 hours as a refresher (as it’s been a little while) so i can refresh on that while learning windows programming…
In this book, which i’ve been tearing through, there’s a chapter on operator overloading. In overloading the assignment operator, they give an example, and say that since some members are allocated on the heap, then this memory must be deleted in order to avoid a memory leak. I don’t understand why that’s necessary. here’s the class code:
1234567891011121314151617181920212223242526272829303132333435class CAT<br />{<br />public:<br />CAT(); //default constructor<br />// copy constructor and destructor elided!<br />int GetAge() const { return *itsAge; }<br />int GetWeight() const { return *itsWeight;}<br />void SetAge(int age) { *itsAge = age; }<br />CAT operator=(const CAT &);<br /><br />private:<br />int *itsAge;<br />int *itsWeight;<br />};<br /><br />CAT::CAT()<br />{<br />itsAge = new int;<br />itsWeight = new int;<br />*itsAge = 5;<br />*itsWeight = 9;<br />}<br /><br />CAT CAT::operator =(const CAT & rhs)<br />{<br />if (this == &rhs)<br />return *this;<br />delete itsAge;<br />delete itsWeight;<br />itsAge = new int;<br />itsWeight = new int;<br />*itsAge = rhs.GetAge();<br />*itsWeight = rhs.GetWeight();<br />return *this;<br />}So i understand the gist of how this works. What i don’t understand is why the deletes are called, where the memory leak comes in, and why not just return a reference CAT. It doesn’t go out of scope, so why not a reference? And why delete the memory, instead of just assigning *itsAge = rhs.GetAge() and *itsWeight = rhs.GetWeight()??? Seems unnecessary.
Here’s what they say:
There is an added wrinkle with the assignment operator, however. The object catTwo already exists and already has memory allocated. That memory must be deleted if there is to be no memory leak. So the first thing you must do when implementing the assignment operator is delete the memory assigned to its pointers.
Another question i have is in operator overloading with the increment operator. i understand the overloading of the prefix operator… but when they go into the postfix increment operator, they say the compiler would have a problem determining the difference between overloading the prefix increment operator with the postfix. Fair enough. It says it differentiates by convention with an integer variable supplied as a parameter to the operator declaration. The parameter’s value is ignored, it’s just a signal that this is the postfix operator. Yet in the implementation, the postfix operator is called in the normal fashion… ie object++; the overlaoding is given as
1const Counter operator++ (int);yet no int is sent in calling it. I guess my question is – is this just some flag for the compiler? Or is there some hidden int actually sent with the ++ operator (i would guess the int would be 1, :) ) – but then if that’s the case, where is the differentiation with prefix and postfix increment – wouldn’t they BOTH be sent an int of 1??? Should i just accept this and move on??? :)
- March 7, 2009 at 5:43 pm #3522AbbeyCarpenterParticipant
i figured it out… the book is wrong! I hate when that happens! Basically, there’s no way there would be a memory leak unless there is no operator overload. If it merely calls the default copy constructor, then yes, catTwo’s member variables would be pointing at the same memory as catOne’s in a catTwo = catOne; scenario.
The copy constructor is called when returning by value, but it doesn’t matter because it’s returning itself. So it’s doing a shallow copy of it’s own variables. Thus no memory leak.
It makes a lot more sense to me to return by reference, since there’s no need for the copy constructor to be called. In fact, it makes more sense to me for the function not to return a value at all, since it does the necessary reassignments within the operator= overload… silly silly example in the book, and it didn’t help that the book was wrong at least in its inference.
- AuthorPosts
- The forum ‘C Programming’ is closed to new topics and replies.