0

I put just print a simple print statement at the constructor and the destructor.

class Complex {
public:
    Complex() {
        cout << "default ctor called\n";
    }
    ~Complex() {
        cout << "dctor called\n";
    }
}
Complex getComplex() {
    Complex temp;
    cout << "----inside function----\n";
    return temp;
}

int main() {
    Complex c = getComplex();
    cout << "----inside main----\n";

    return 0;
}

expected output:

----inside function----
default ctor called
destructor called
----inside main----

Actual output

----inside function----
default ctor called
----inside main----
destructor called

Why when the function returned the destructor didn't call at function? Actually, it doesn't make sense because I learned that when declaring any variable at a function, it ends when the function is returned.

user438383
  • 5,716
  • 8
  • 28
  • 43
Run
  • 130
  • 6

0 Answers0