构造与析构函数是否可以是虚函数
1 /* 构造与析构的虚函数问题 */
2
3 #include<iostream>
4
5 using namespace std;
6
7 class ye
8 {
9 public:
10 ye()// 禁止构造函数是虚函数
11 {
12 p1 = new char[1024*1024*100];//100M
13 cout << "ye来了" << endl;
14 }
15
16 virtual ~ye()// 析构函数可以是虚函数,解决内存泄露问题
17 {
18 delete []p1;
19 cout << "ye走了" << endl;
20 }
21
22 protected:
23 char *p1;
24 };
25
26
27 class die:public ye
28 {
29 public:
30 die()
31 {
32 p2 = new int[1024*1024*100];//400M
33 cout << "die来了" << endl;
34 }
35
36 ~die()
37 {
38 delete []p2;
39 cout << "die走了" << endl;
40 }
41
42 protected:
43 int *p2;
44 };
45
46 void main()
47 {
48
49 ye *p = new die;
50 delete p;
51
52
53 cin.get();
54 }
长风破浪会有时,直挂云帆济沧海
posted on 2015-06-10 09:53 Dragon-wuxl 阅读(130) 评论(0) 收藏 举报
浙公网安备 33010602011771号