为了账号安全,请及时绑定邮箱和手机立即绑定

执行到析构函数怎么出错了?

#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;
 }


析构函数只能执行一次,理应是三次  而且这一次还出错了 https://img1.sycdn.imooc.com//5b8006f400016f8c15590732.jpg

正在回答

1 回答

删掉

 delete []m_strName1;
   m_strName1=NULL;

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
C++远征之封装篇(上)
  • 参与学习       103381    人
  • 解答问题       732    个

封装--面向对象的基石,本教程力求帮助小伙伴们即学即会

进入课程

执行到析构函数怎么出错了?

我要回答 关注问题
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号