C++不是说的默认析构函数不能删除new 操作符分配出来的对象或对象成员么? 但是为什么这里好像可以呢?
classCBox{char*pmessage;public:CBox(char*message){cout<<"CBox(char*message)runing!";p...
class CBox{
char* pmessage;
public :
CBox(char* message){
cout<<"CBox(char* message) runing!";
pmessage=new char[strlen(message)+1];
strcpy_s(pmessage,strlen(message)+1,message);
}
void ScanMessage(){
cout<<'\n'<<pmessage;
}
~CBox(){
cout<<"\n ~CBox() has been runing!";
}
};
int main(void){
CBox *pbox;
pbox=new CBox("My name");
pbox->ScanMessage();
delete pbox;
return 0;
}
显示的结果为CBox(char* message) runing!";
My name
~CBox() has been runing!"
这里虽然显示了~CBox() has been runing!",但是不是只能说明它被调用了,并不表示他释放了CBox类中pmessage分配出来的内存呢?
另外,如果它没有释放,那是不是即使程序结束了,pmessage分配出来的内存任然未被释放呢? 展开
char* pmessage;
public :
CBox(char* message){
cout<<"CBox(char* message) runing!";
pmessage=new char[strlen(message)+1];
strcpy_s(pmessage,strlen(message)+1,message);
}
void ScanMessage(){
cout<<'\n'<<pmessage;
}
~CBox(){
cout<<"\n ~CBox() has been runing!";
}
};
int main(void){
CBox *pbox;
pbox=new CBox("My name");
pbox->ScanMessage();
delete pbox;
return 0;
}
显示的结果为CBox(char* message) runing!";
My name
~CBox() has been runing!"
这里虽然显示了~CBox() has been runing!",但是不是只能说明它被调用了,并不表示他释放了CBox类中pmessage分配出来的内存呢?
另外,如果它没有释放,那是不是即使程序结束了,pmessage分配出来的内存任然未被释放呢? 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询