Home Forums C Programming Question on operator overloading… and memory management

Viewing 1 reply thread
  • Author
    • #2179

      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:

      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

      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??? :)

    • #3522

      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.

Viewing 1 reply thread
  • The forum ‘C Programming’ is closed to new topics and replies.