I'm trying to write a function for the +=
operator of a C++ class which utilizes the already written +
operator function. So far, I have been unsuccessful in associating the this
pointer with the +
operator. These are some of the attempts I've made that compiled in g++
but did not produce the desired result. Twice I attempted simply to make a copy of the this
class, but that did not appear to work.
intstr& intstr::operator+=(const intstr& i)
{
intstr *a;
a = new intstr;
*a = *this + i;
return *a;
}
intstr& intstr::operator+=(const intstr& i)
{
intstr *a, b(*this);
a = new intstr;
*a = b + i;
return *a;
}
intstr& intstr::operator+=(const intstr& i)
{
intstr *a, *b;
a = new intstr;
b = this;
*a = *b + i;
return *a;
}
intstr& intstr::operator+=(const intstr& i)
{
intstr *a;
a = new intstr;
*a = this->operator+(i);
return *a;
}
In the test code, all I've done is replace the working line of code a = a + i
with a += i
, so I doubt the problem lies there, but it is possible. Is the only way to do this to copy the code from the +
operator into the +=
function?