Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C/++/# Assignment Operator Overloading Question

  1. Mar 30, 2017 #1
    Hey everyone. First I am new to programming (so my vocabulary and skills are not very proficient with C++), and I'm learning operator overloading. I have questions about line 1 and lines 5-8 of this code.
    Mod note: changed quote tags to code tags to preserve indentation.
    Code (C):

    NumberArray& NumberArray :: operator=(const NumberArray &right)
        if (this != &right)
          if (arraySize > 0)
              delete [] aPtr; // this is a pointer defined in the class
          arraySize = right.arraySize;
          aPtr = new double[arraySize];
          for (int index = 0; index < arraySize; index++)
            aPtr[index] = right.aPtr[index];
        return *this;
    This is sample of a member function in a much larger program. The goal of the program is to demonstrate that during assignment you have to use operator overloading as opposed to a construction operator in initialization. The point of it is if I assigned the following:

    NumberArray a, b;
    a = {1, 2, 3}
    b = {2}
    a = b

    That a should be 'a' should now be assigned '2'. And this code accomplishes that. However, in line 1 it's returning a reference to NumberArray., and then on line 16, it's using the dereference operator to return the data of stored in the pointer 'this.' My question is, why should I return a reference, and where is this returning the value to?

    Secondly, on line 7, why should I want to delete this from the heap? If I reassign my values in lines 11-15, doesn't this just overwrite the memory since I'm using the pointer in those lines? So it doesn't appear to me that I'm creating any memory leaks.

    I'm asking this because I've removed all of these from the code and it still runs fine.

    Last edited by a moderator: Mar 30, 2017
  2. jcsd
  3. Mar 30, 2017 #2


    User Avatar

    Typical reasons for returning a reference are one wants to make sure the returned object actually exists and stays defined until the object goes out of scope. These are pretty useful things to have the complier guarantee for an assignment operator.

    The memory is deleted on line 7 because aPtr is overwritten on line 10. This means we will loose the address of the old memory after the call to new which is a leak. The test code sequence given works with lines 7 and 10 removed because a has more elements than b. If you remove lines 7 and 10 and change the test assignment to b=a the program will crash.
  4. Mar 30, 2017 #3
    So is this line essentially checking that if aPtr has been defined, then it will have some assigned memory value on the heap? Thus if it has assigned memory value, it wants to delete it since were defining new memory on the heap of an array that may potentially be a different size in line 10?

    In other words I'll have two different memory locations on the heap of differing sizes, where one of them I am no longer using? I guess what it boils down to is why can't I just rewrite this memory on the heap? Is it because my array sizes may vary?

    Sorry, my book I'm using just went from fairly easy concepts (for me) to grasp to going concepts on crack since it hit the pointers chapter.
  5. Mar 30, 2017 #4


    Staff: Mentor

    You can rewrite memory on the heap, no problem, but you need to be careful not to write beyond the limits of this memory, in either direction. If the array sizes are different (or could be), that's a good reason for doing separate heap allocations and being careful to free the memory when it's no longer used.
  6. Mar 30, 2017 #5


    User Avatar

    Yes, that is why.

    If the object right has more elements than can fit in the memory pointed to by aPtr then we need to create a new memory region which is large enough. If right has less elements then one doesn't necessarily have to do the new/delete operations on aPtr, and in some cases that optimization could make sense, but this way is a lot easier and is always correct so that's probably why the author selected it.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted