overloading operator+

So what i'm trying to do, is add a number to an array in my object, and if needed, allocate more memory to it.

my directions are:

Create a pointer to an integer. -done

Allocate an array of integers that is twice the size of the original set. The address that is returned from new should be saved in the pointer that was created above. -done

Copy the values from the original set into the new array. -done

Free the memory for the original set. -done

Save the address of the new array in the pointer to an integer data member -????

update the set's maximum capacity data member -????

here's the definition so far:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Set operator +(const Set& right, const int& num)
{
    int * address;
    if (right.numElements == right.maxcap)
    {
        &address = new Set;
        Set twice(right.numElements * 2);
        twice.numElements = right.numElements;
        int i;
        for(i = 0; i < right.numElements; i++)
        {
            twice.ar[i] = right.ar[i];
        }
        delete right.ar;
        *twice.ar = &address;
    }
    else
    {
        &right.numElements = right.numElements + 1;
        right.ar[right.numElements] = num;
        *address = *right.ar;
    }
    return *address;
}


And here's the data members for the "Set" class
1
2
3
4
5
6
7
class Set
{   
    private:
        int numElements;
        int* ar;
        int maxcap;
}


I'm having trouble storing the new array back into the original "right", as well as understanding how to use the pointers to change the addresses and some non-lvalue errors.
Last edited on
Why would you want to change the original "right" (which is actually the lhs)? The + operator should not be modifying it's operands.
Topic archived. No new replies allowed.