-4

I need to implement the overloading of the += operator in c++. I have a Vector class and my implentation of += needs to add an integer from the back of my Vector.

For example: if my vector V1 contains of {1, 2, 3} and I write V1 += 4, it needs to give me {1, 2, 3, 4}.

I have almost the same functionality with the overload of the + operator which receives a Vector and an int and adds the given integer to the back of the array.

Fields for my class

    class Vector
    {
        unsigned int _size;
        int * _list;

HERE IS MY NON-WORKING SOLUTION FOR += OPERATOR

    Vector& operator+=(const int val)
    {
        Vector temp;
        temp._list = new int[_size + 1];
        temp._size = this->_size + 1;
    
        for (unsigned int i = 0; i < temp._size; i++)
        {
            if (i < temp._size - 1)
            {
                temp._list[i] = this->_list[i];
            }
            else if (i == temp._size - 1)
            {
                temp._list[i] = val;
            }
        }
        return temp;
    }

AND ALMOST IDENTICAL WORKING SOLUTION FOR + OPERATOR

    friend Vector operator+(Vector lhs, const int val)
        {
            Vector temp;
            temp._list = new int[lhs._size + 1];
            temp._size = lhs._size + 1;
    
            for (unsigned int i = 0; i < temp._size; i++)
            {
                if (i < temp._size - 1)
                {
                    temp._list[i] = lhs._list[i];
                }
                else if (i == temp._size - 1)
                {
                    temp._list[i] = val;
                }
            }
            return temp;
        }

I can't understand where is the key difference, however I am guessing it is somewhere in & or friend concepts, cuz I dont'really understand how they are working in this case.

And one more thing I MUST NOT CHANGE THE DESCRIPTION OF MY METHODS. (just the implementation)

Alan Birtles
  • 32,622
  • 4
  • 31
  • 60
  • *"HERE IS MY NON-WORKING SOLUTION..."* What does nonworking mean? Do you get any error. Or something else. – Jason Jul 21 '22 at 11:40
  • 2
    You can't return a reference to a local variable. – Some programmer dude Jul 21 '22 at 11:41
  • your `operator+=` is broken due to returning a reference to a local. – 463035818_is_not_an_ai Jul 21 '22 at 11:41
  • I also suggest you spend some time to read [this canonical implementation reference](https://en.cppreference.com/w/cpp/language/operators#Canonical_implementations) for overloading operators. From it you should learn that all assignment operators should return a reference to `*this` (and modify `this`, not create a new object). – Some programmer dude Jul 21 '22 at 11:41
  • Unnecessarily duplicated code. `Vector& operator+=(const int val) { *this = *this + val; return *this; }` would also work. – pptaszni Jul 21 '22 at 11:43
  • 1
    @pptaszni Generally, the binary operators should be implemented from the compound assignment operators, not the other way around. So `+` should be implemented using `+=`. – Some programmer dude Jul 21 '22 at 11:46
  • Suggest thinking about using another operator for this (e.g. `<<=`). For `+=`, one might expect to add rhs to each element of lhs. – lorro Jul 21 '22 at 11:46
  • `operator+=` should modify and return `this` not a temporary value – Alan Birtles Jul 21 '22 at 11:57
  • A note about the code: if you change the upper limit of the loop so it runs up to `temp._size - 1` instead of `temp._size` you can make the code much simpler: `for (unsigned i = 0; i < temp._size - 1; ++i) { temp._list[i] = lhs._list[i]; } temp._list[temp._size - 1] = val;`. – Pete Becker Jul 21 '22 at 12:05
  • Wouldn't it be idiomatic to implement this as `push_back`? – Paul Sanders Jul 21 '22 at 14:35
  • In the expression `a += b`, the effect is modifying `a` and the result is (typically a reference to) the modified `a`. In the expression `a + b`, it is pretty typical that the effect does not involve modifying either `a` or `b`, and the result is another (temporary) object containing the result of summation. – Peter Jul 21 '22 at 16:08

1 Answers1

1

In the += operator, you should set the new value to the vecor to be added itself.

Also note that returning a reference to a non-static local variable temp is a bad idea.

Try this:

Vector& operator+=(const int val)
{
    Vector temp;
    temp._list = new int[_size + 1];
    temp._size = this->_size + 1;

    for (unsigned int i = 0; i < temp._size; i++)
    {
        if (i < temp._size - 1)
        {
            temp._list[i] = this->_list[i];
        }
        else if (i == temp._size - 1)
        {
            temp._list[i] = val;
        }
    }
    // move the data in temp to this object
    this->_size = temp._size;
    delete[] this->_list;
    this->_list = temp._list;
    // assign new array to temp for being destructed
    temp._list = new int[1];
    temp._size = 1;
    // return a reference to this object
    return *this;
}
MikeCAT
  • 73,922
  • 11
  • 45
  • 70