[try Beta version]
Not logged in

 
Destructor

Jun 18, 2013 at 11:16am
Write your question here.

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
 #include<iostream>

using namespace std;
class test
{
	int i;
public:
	test(int k = 7) {
		i = k;
		cout<<"C "<<i<<endl; 
	}
	~test() { cout<<"D "<<i<<endl; }
};

int main()
{
	test b;
	{
		static test k(2);
		test m(3);
	}
	return 0;
}



please, explane me why destructor works this way ?
Jun 18, 2013 at 11:29am
Which way does it work?:)
Its work depends on the storage duration. For example for all variables with the static storage duration

The storage for these entities shall last for the duration of the program
(The C++ Standard)

So in your example variable b has storage duration of function main. Variable m has storage duration of the internal compound statement. And variable k has storage duration of the program.
So the order of calling destructors will be 1) the destructor of m, 2) the destructor of b 3) and at last the destructor of k.
Last edited on Jun 18, 2013 at 11:39am
Topic archived. No new replies allowed.