For an object, its destructor is called when it goes out of scope. Here's my program:
#include <iostream>
using namespace std;
class A {
public:
A() {}
~A() { cout << "A's destructor called!" << endl; }
};
class B {
public:
B() = delete;
B(int x_) {}
~B() { cout << "B's destructor called!" << endl; }
};
void func_a() { A a(); }
void func_b() { B b(1); }
int main() {
cout << "A" << endl;
func_a();
cout << "B" << endl;
func_b();
cout << "bye" << endl;
return 0;
}
I thought either A's or B's destructor would be called. But the fact is only B's destructor called. I'm very confused. Is this because of the storage way of the class? Output:
A
B
B's destructor called!
bye