奇怪了,vector组件中这样不可以?为什么?
#include <iostream>#include <string>
#include <vector>
using namespace std;
int i=0;
class CDemo
{
public:
CDemo():str(NULL)
{}
/* CDemo(const CDemo &cd)
{
this->str=new char[strlen(cd.str)+1];
strcpy(str,cd.str);
}*/
~CDemo()
{
if(str)
{
delete []str;
}
}
char *str;
};
int main(int argc,char *argv[])
{
CDemo d1;
d1.str=new char[32];
strcpy(d1.str,"trend micro");
vector<CDemo>a1;// = new vector<CDemo>();
/*for(i=0;i<10;i++)
{
a1.push_back(d1); //循环第3次的时候崩?????????
}
//a1->push_back(d1);
for(i=0;i<10;i++)
{
cout<<a1[i].str<<endl;
}
*/
CDemo d2(d1);
CDemo d3(d1);
CDemo d4(d1);
CDemo d5(d1);
a1.push_back(d1);
a1.push_back(d2);
a1.push_back(d3);
a1.push_back(d4); ///////////////////////崩处!!!
a1.push_back(d5);
//delete a1;
return 0;
}
这几个对象公共一个字符串空间,我给vector加入的是对象,又没释放内存的,怎么会在添加d4的时候系统崩了呢?这是为什么呀????