类对象数组 被析够 的时候出现 非法错误
#include<iostream>#include<vector>
using namespace std;
class Student
{
public:
Student();
Student(const char*);
Student(Student&);
char* read() {return _name;}
~Student();
private:
char* _name;
};
Student::Student():_name(0)
{
cout<<"Student Defult Constructing called..."<<endl;
}
Student::Student(const char* name)
{
cout<<"Student Constructing called..."<<name<<endl;
_name = new char[strlen(name) + 1];
strcpy(_name,name);
}
Student::Student(Student& p)
{
cout<<"Student Copying Constrcting called... :"<<p._name<<endl;
_name = new char[strlen(p._name) + 1];
strcpy(_name,p._name);
}
Student::~Student()
{
cout<<"Student Destructing called...: "<<_name<<endl;
delete _name;
}
int main()
{
Student a[3] = {"Jones","Anna","Tom"};
Student b[3];
return 0;
}
怎样才能避免出现这样的情况?