I've searched around and can't seem to find an answer to my question. I am doing a project where I have to redefine some operators (+
, -
, *
, etc.) for operations between vectors and polynomials. As far as I know, those operators are supposed to return copies of the objects so as to not modify them directly if we're simply calling the operator (ie vect1+vect2;
instead of vect1 += vect2;
) and not placing the result anywhere.
Now, I've seen all around that using static variables is a bad practice, but how can I avoid doing that when a method needs to return a copy of the result instead of modifying the object?
The questions here and here didn't really help because they don't address my particular issue.
Here is an example of what I mean:
template <class elem>
Vect_variable<elem>& Vect_variable<elem>::operator+(const Vect_variable& operand)
{
if (taille >= operand.taille)
{
static Vect_variable<elem> temp_v;
temp_v = *this;
for (std::size_t i = 0; i<operand.taille; i++)
{
temp_v[i] += operand.vecteur[i];
}
return temp_v;
}
else
{
static Vect_variable<elem> temp_v;
temp_v = operand;
for(std::size_t i = 0; i<taille; i++)
{
temp_v[i] += vecteur[i];
}
return temp_v;
}
}
In this case you can see that I am creating static Vect_variable
for the temporary variable used. Is there any way to do this otherwise?