為什么又的析構(gòu)函數(shù)里面加delete
int main(void)
{
? ? // 通過new方式實(shí)例化對象*stu
? ? Student *stu = new Student();
? ? // 更改對象的數(shù)據(jù)成員為“慕課網(wǎng)”
? ? stu->setName("慕課網(wǎng)");
cout<<stu->getName()<<endl;
? ? // 打印對象的數(shù)據(jù)成員
Student stu1;
cout<<stu1.getName()<<endl;
Student stu2("marry");
cout<<stu2.getName()<<endl;
Student Stu3(stu1);
cout<<Stu3.getName()<<endl;
delete stu;
stu=NULL;
return 0;
}
?這樣寫有問題嗎
2020-10-03
這樣寫沒有問題。析構(gòu)函數(shù)的作用和手動delete對象釋放空間一樣,不會沖突。