When main()
writes return
, the destructor of the local object is called.
Because exit()
does not return, the destructor of the local object is not called. (In some special cases, such as files, the static
object will vanish.)
abort()
does not destroy any objects (including global
/static
.
#include <iostream>
using namespace std;
class Foo{
public:
int num;
Foo(intn): num(n) { cout << "Call creator: " << num << endl;}
~Foo() { cout << "Call the extinct: " << num << endl;}
};
Foo foo0(0);
int main(){
Foo foo1(1);
return 0;
}
Output:
Call constructor: 0
Invoke constructor: 1
Calling extinction: 1
Destructor call: 0
#include <iostream>
using namespace std;
class Foo{
public:
int num;
Foo(intn): num(n) { cout << "Call creator: " << num << endl;}
~Foo() { cout << "Call the extinct: " << num << endl;}
};
Foo foo0(0);
int main(){
Foo foo1(1);
exit(0);
}
Output:
Call constructor: 0
Invoke constructor: 1
Destructor call: 0
© 2024 OneMinuteCode. All rights reserved.