c++STL存对象析构问题
class temp
{
public:
char *p;
temp(char *lhs)
{
//p=lhs; 在这里动态分配与静态分配好像一样的???
p=new char[strlen(lhs)+1];
strcpy(p,lhs);
}
~temp()
{
delete []p;//如果不将对象放入容器当中那结果正确,但放入容器就显示错误
//是不是放入容器中后就会将对象中的资源自动释放掉???
}
};
int main()
{
temp tmp("wkl");
temp tmp2("www");
vector<temp> vec;
vec.push_back(tmp);
vec.push_back(tmp2);
cout<<vec[0].p<<endl;
cout<<vec[1].p<<endl;
return 0;
}