关于指针析构的问题
//类的声明和定义class Student
{
public:
void Display();
Student(int num,char *name);
virtual ~Student();
protected:
int m_nNum;
char *m_pstrName;
const char m_cGender;
};
Student::Student(int num,char *name):m_cGender('F')
{
m_nNum = num;
char *m_pstrName = new char;
strcpy(m_pstrName, name);
}
Student::~Student()
{
delete m_pstrName;
}
void Student::Display()
{
cout << "Number is " << m_nNum << endl;
cout << "Gender is " << m_cGender << endl;
cout << "Name is " << m_pstrName << endl;
}
//主函数
#include <iostream>
#include "Student.h"
using namespace std;
int main()
{
Student st(11,"wangj");
st.Display();
return 0;
}
Display中前两句输出能顺利执行,但执行到后一句时
系统总是提示:"0x00421980"指令引用的"0xcccccccc"内存.该内存不能为"read".
[[it] 本帖最后由 wjcloudy 于 2008-6-25 16:50 编辑 [/it]]