0

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;
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
Festik
  • 1
  • 1
  • Sidenote: `the main` contains nonsense.`double arrary[] = {...};` and then `num2(arr, 3);` Post code that compiles instead. – Ted Lyngmo Sep 01 '22 at 21:38
  • `operator +` should be returning a `test2`, not a reference. Second, fix your `test2` class so that it has proper copy semantics: `int main() { test2 t1(10); test2 t2 = t1; }` -- That simple program has a double-delete error. – PaulMcKenzie Sep 01 '22 at 21:38
  • 1
    [What is the rule of 3?](https://stackoverflow.com/questions/4172722/what-is-the-rule-of-three). At that link, go to the **Managing resources** section and read it carefully. The code sample is almost identical to yours, and shows the mistakes you are making with respect to copying. – PaulMcKenzie Sep 01 '22 at 21:46
  • You've now edited the question so that the answer you've gotten doesn't make sense. Please don't do that. I rolled back that edit. Put your original code in the question + addi a compileable version of `main` etc. – Ted Lyngmo Sep 02 '22 at 10:32

1 Answers1

2

You here return a local variable by reference:

    test2& operator+(double d) {   // test2&
        test2 newTest(size);       // local variable

        for (int i = 0; i < size; i++) {
            newTest[i] = array[i] + d;
        }

        return newTest;
    }

Since newTest is destroyed when it goes out of scope, you return a dangling reference. Accessing it will make the program have undefined behavior - and could do anything.

Make it return by value instead:

    test2 operator+(double d) {
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
  • 1
    And if you're worried about returning a `test2` by value and incurring costs copying it, 1) Modern C++ compilers are really good at eliminating the copy to the point that it is [now mandatory in many common cases](https://en.cppreference.com/w/cpp/language/copy_elision) and 2) fast code that doesn't work ain't worth . – user4581301 Sep 01 '22 at 22:35
  • Thanks for your answers! Well, I changed the method to return by value, and still the destructor destroyes the array before it goes back to the main. And again I have the undefined behavior – Festik Sep 02 '22 at 09:34
  • @Festik Please update your question and put a compileable [mre] in it and I'll point out where the mistakes are and provide a fix for them. – Ted Lyngmo Sep 02 '22 at 09:50