c++运算符重载的问题。。头疼啊
#include<iostream>#include<cstring>
using namespace std;
class st
{
private:
int x,y;
char *name;
public:
st(const int x1,int y1,char *name1)
{
x=x1;y=y1;
name=new char[strlen(name1)+1];
strcpy(name,name1);
}
st()
{
name=NULL;
x=y=0;
}
st &operator=(const st &p);
~st()
{
delete[]name;
}
void print()
{
cout<<x<<endl<<y<<endl<<name<<endl;
}
};
#include"eight.h"
st &st::operator=(const st&p)(为什么COM1不给初始化,程序就不执行这一段代码啊?,请教老鸟仔细的说一下)
{
if(this!=&p)
{
x=p.x;y=p.y;
delete []name;
name=new char[strlen(p.name)+1];
cout<<"调用复制构造函数"<<endl;
if(name!=NULL)
{
strcpy(name,p.name);
return (*this);
}
}
else
{
cout<<"没改变"<<endl;
return (*this);
}
}
int main()
{
st com(1,2,"xbx");
st com1=com;
com1.print();
}