執行到析構函數怎么出錯了?
#include<iostream>
#include<string>
using namespace std;
class Student
{
?public:
??Student()
??{
???m_strName1=new char[20];
??}
??Student(string _name,int _age)
??{
???m_strName2=_name;
???m_intAge2=_age;
??}
??~Student()
??{
???cout<<"已運行析構函數"<<endl;
???delete []m_strName1;
???m_strName1=NULL;
??}
??Student(const Student &kelong):m_strName3("duoli"),m_intAge3(1)
??{
???cout<<"已運行拷貝構造函數"<<endl;
??}
??void setName()
??{
???cin>>m_strName1;
??}
??void getInformation1()
??{
???cout<<m_strName1<<endl;
??}
??void getInformation2()
??{
???cout<<m_strName2<<m_intAge2<<endl;
??}
??void getInformation3()
??{
???cout<<m_strName3<<m_intAge3<<endl;
??}
?private:
??char *m_strName1;
??string m_strName2;
??string m_strName3;
??int m_intAge2;
??int m_intAge3;
};
int main()
{
?Student t1;
?Student t2("baoyanghao",19);
?Student t3(t1);
?
?t1.setName();
?t1.getInformation1();
?t2.getInformation2();
?t3.getInformation3();
?return 0;
?}
析構函數只能執行一次,理應是三次? 而且這一次還出錯了?
2018-08-25
刪掉
?delete []m_strName1;
???m_strName1=NULL;