求助一个C++的问题 下面这个程序注释掉Employee类中的两行delete,不注释掉为什么运行
求助一个C++的问题下面这个程序注释掉Employee类中的两行delete,不注释掉为什么运行报错?#include<iostream>usingnamespacest...
求助一个C++的问题
下面这个程序注释掉Employee类中的两行delete,不注释掉为什么运行报错?
#include<iostream>
using namespace std;
class Employee
{ public:
Employee(char*name,char*no)
{ m_name=name; m_no=no; }
~Employee()
{ // delete []m_no; // delete []m_name; }
virtual void Display()
{ cout<<"姓名"<<m_name<<"员工号"<<endl<<m_no<<endl; }
char* getname() { return m_name; } char* getno() { return m_no; }
private: char*m_name;
char*m_no;
};
class Leader:public Employee
{ private: char*m_posdes;
public:
Leader(char*name,char*no,char*posdes):Employee(name,no)
{ m_posdes=posdes; }
~Leader() { delete []m_posdes; }
virtual void Display()
{
cout<<"姓名"<<getname()<<endl<<"员工号"<<getno()<<endl<<"职位描述"<<m_posdes<<endl;
}
};
int main()
{
Employee*pe=new Leader("张三","1011","软件开发部部门经理");
pe->Display();
delete pe;
return 0;
} 展开
下面这个程序注释掉Employee类中的两行delete,不注释掉为什么运行报错?
#include<iostream>
using namespace std;
class Employee
{ public:
Employee(char*name,char*no)
{ m_name=name; m_no=no; }
~Employee()
{ // delete []m_no; // delete []m_name; }
virtual void Display()
{ cout<<"姓名"<<m_name<<"员工号"<<endl<<m_no<<endl; }
char* getname() { return m_name; } char* getno() { return m_no; }
private: char*m_name;
char*m_no;
};
class Leader:public Employee
{ private: char*m_posdes;
public:
Leader(char*name,char*no,char*posdes):Employee(name,no)
{ m_posdes=posdes; }
~Leader() { delete []m_posdes; }
virtual void Display()
{
cout<<"姓名"<<getname()<<endl<<"员工号"<<getno()<<endl<<"职位描述"<<m_posdes<<endl;
}
};
int main()
{
Employee*pe=new Leader("张三","1011","软件开发部部门经理");
pe->Display();
delete pe;
return 0;
} 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询