I'm new to c++ and I have tested few classes with overloading operators and I have an issue: I don't understand why, but the destructor called after I used the '+' operator in main: (Thanks in advance!)
class test2 {
double* array;
int size;
test2() {
}
test2(int n) {
size = n;
array = new double[size];
for (int i = 0; i < size; i++)
{
array[i] = 0;
}
}
double& operator[](int index) {
return array[index];
}
test2& operator+(double d) {
test2 newTest(size);
for (int i = 0; i < size; i++) {
newTest[i] = array[i] + d;
}
return newTest;
}
~test2() {
delete[] array;
}
};
the main:
double arrary[] = { 1.1, 1.2, 1.3};
test2 num2(arr, 3);
test2 num3 = num2 + 2;