-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDestructor.cpp
More file actions
42 lines (34 loc) · 797 Bytes
/
Destructor.cpp
File metadata and controls
42 lines (34 loc) · 797 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include<vector>
#include<iostream>
using namespace std;
class A {
int i;
public:
A(int i) {
this->i = i;
cout<<"A Constructed with i = "<< i <<endl;
}
~A() {
cout<<"A Destructed with i = " << i <<endl;
}
};
A global(0);
int main() {
A obj[] = {1,2,3};
vector<A> vect;
vect.push_back(A(4));
vect.push_back(A(5));
vect.push_back(A(6));
// A(4) never destructed
A* ptr1 = new A(7);
A* ptr2 = new A(8);
// Uncommenting below statement will not destruct 0 (global variable)
// throw exception();
{
// Deleted first since it's block ends first
A obj(9);
}
// delete ptr does two things - first calls the destructor and then deallocates memory.
delete ptr2;
return 0;
}