Here is an example of a class that is made available for the +
operation.
class A
{
public:
int *array;
A()
{
array = new int[10];
}
~A()
{
delete[] array;
}
A operator+ (const A &b)
{
A c;
for(int i=0; i<10; i++)
c.array[i] += array[i] + b.array[i];
return c;
}
};
int main()
{
A a,b,c,d;
/* puts some random numbers into the arrays of b,c and d */
a = b+c+d;
}
Will a
run the destructor before copying the result of b+c+d
or not? If not, how do I make sure no memory is leaked?
The +
operator overload is designed this way such that no operand is modified.