在使用拷贝构造函数的时候出现错误了!
编译和连接都没发现错误,在运行的时候弹出一个对话框,错误。但是我没看出来哪里有错误啊!!
源代码:
#include <iostream.h>
#include <string.h>
class Student
{
char *Name;
int Age;
public:
Student(char *namep, int age)
{
Age = age;
if (namep)
{
Name = new char[strlen(namep+1)];
strcpy(Name, namep);
}
else
Name = NULL;
}
Student(Student &s)
{
Age = s.Age;
if (s.Name)
{
Name = new char[strlen(s.Name)+1];
strcpy(Name, s.Name);
}
else
Name = NULL;
}
void Show()
{
cout << Name << ',' << Age << endl;
}
~Student()
{
if (Name)
delete [] Name;
}
};
void main()
{
Student a("George", 20);
Student b=a;
b.Show();
}
[[italic] 本帖最后由 zhiqiang 于 2007-12-1 21:15 编辑 [/italic]]