下面的代码是面试宝典第9章,面试例题2,95页的内容,但是我运行时并没有出现书中所说的错误,记录在此。
View Code
1 #include "stdafx.h"
2 #include <iostream>
3 #include <vector>
4 using namespace std;
5
6 class CDemo{
7 public:
8 CDemo():str(NULL){};
9 ~CDemo()
10 {
11 if(str){
12 static int i=0;
13 cout<<"&CDemo"<<i++<<"="<<(int *)this<<",str'="<<(int *)str<<endl;
14 delete[] str;
15 }
16 };
17 char* str;
18 };
19
20 int _tmain(int argc, _TCHAR* argv[])
21 {
22 CDemo dl;
23 dl.str=new char[32];
24 strcpy(dl.str,"hello world");
25 vector<CDemo> *al=new vector<CDemo>();
26 al->push_back(dl);
27
28 delete al;
29 delete dl;
30 getchar();
31 getchar();
32 return 0;
33 }