Reputation: 21
I have a dynamic array and I need to make a function which overloads the operator + such that it should give you the possibility to add a new object element to the array in two ways:
array=array+element; and array=element+array;
Until now I have the function:
DynamicVector& DynamicVector::operator+(const TElement& e)
{
if (this->size == this->capacity)
this->resize();
this->elems[this->size] = e;
this->size++;
return *this;
}
But this only works for the first case, when we do array=array+element;
How can I implement the problem to work for both the cases.
Upvotes: 1
Views: 53
Reputation: 598414
First off, operator+
is not supposed to modify the left-hand object being added to. It is supposed to return a new object that is a copy of the two inputs added together, eg:
DynamicVector DynamicVector::operator+(const TElement& e) const
{
DynamicVector tmp(*this);
if (tmp.size == tmp.capacity)
tmp.resize();
tmp.elems[tmp.size] = e;
tmp.size++;
/* NOTE: the above can be optimized to avoid unnecessary reallocations:
DynamicVector tmp;
tmp.capacity = size + 1; // or rounded up to some delta...
tmp.size = size + 1;
tmp.elems = new TElement[tmp.capacity];
std::copy(elems, elems + size, tmp.elems);
tmp.elems[size] = e;
*/
return tmp;
}
You are thinking of operator+=
instead, which is supposed to modify the object being added to, and return a reference to it:
DynamicVector& DynamicVector::operator+=(const TElement& e)
{
if (size == capacity)
resize();
elems[size] = e;
size++;
return *this;
}
Now, that being said, to answer your question, you need to define a separate non-member overload of operator+
to handle the case where TElement
is on the left-hand side, eg:
DynamicVector operator+(const TElement& e, const DynamicVector &v)
{
return v + e;
}
Or:
DynamicVector operator+(const TElement& e, const DynamicVector &v)
{
DynamicVector tmp(v);
tmp += e;
return tmp;
/* alternatively:
return DynamicVector(v) += e;
*/
/* NOTE: the above can be optimized to avoid unnecessary reallocations:
DynamicVector tmp;
tmp.capacity = v.size + 1; // or rounded up to some delta...
tmp.size = v.size + 1;
tmp.elems = new TElement[tmp.capacity];
std::copy(v.elems, v.elems + v.size, tmp.elems);
tmp.elems[v.size] = e;
return tmp;
*/
}
Upvotes: 1
Reputation: 206747
How can I implement the problem to work for both the cases.
You need to overload the function as a non-member function.
DynamicVector& operator+(const TElement& e, DynamicVector& v);
Ideally, make both of them non-member functions.
You can implement the second one using the first one.
DynamicVector& operator+(const TElement& e, DynamicVector& v)
{
return v + e;
}
Suggestions for improvement.
operator+=
member function to DynamicVector
.operator+
functions work with const
objets.Member function.
DynamicVector& DynamicVector::operator+=(const TElement& e)
{
if (this->size == this->capacity)
this->resize();
this->elems[this->size] = e;
this->size++;
return *this;
}
Non-member functions.
DynamicVector operator+(DynamicVector const& v, TElement const& e)
{
DynamicVector copy(v);
return (copy += e);
}
DynamicVector operator+(TElement const& e, DynamicVector const& v)
{
return v + e;
}
With those changes the overloaded operators work just like fundamental types.
int i = 0;
i += 3; // Modifies i
int j = i + 10; // Does not modify i. It creates a temporary.
Upvotes: 2