求助,高手看一下我的程序那里错了!!!
本人用的是vs2010,下面是代码:#include <iostream>
#include <string.h>
using namespace std;
class CDemo
{
public:
CDemo(char *s)
{
ps=new char(strlen(s)+1);
strcpy(ps,s);
}
~CDemo()
{
if(ps) delete[] ps;
}
void print()
{
cout<<ps<<endl;
}
CDemo& operator =(CDemo &a)
{
if(ps) delete[] ps;
if(a.ps){
ps=new char(strlen(a.ps)+1);
strcpy(ps,a.ps);
}
else ps =0;
return *this ;
}
private:
char *ps;
};
void main()
{
CDemo d1("Key"),d2("Mouse");
d1=d2;
d1.print();
}
没有语法错误,可以运行就显示错误。谢谢了!!!