拷贝构造函数的问题
#include <string.h>
#include <iostream.h>
class name
{
public:
name()
{
pname=0;
cout<<"默认构造函数"<<endl;
}
name(char* pn)
{
copyname(pn);
cout<<"构造函数"<<endl;
}
name(name& s)
{
cout<<"拷贝构造"<<endl;
copyname(s.pname);
}
~name()
{
//deletename();
cout<<"析构";
}
name& operator=(name& s)
{
//deletename();
copyname(s.pname);
cout<<"赋值运算符"<<endl;
return *this;
}
void display()
{
cout<<pname<<endl;
}
protected:
void copyname(char * pn);
// void deletename();
char* pname;
};
void name::copyname(char* pn)
{
pname=new char[strlen(pn)+1];
if(pname)
{
strcpy(pname,pn);
}
}
/*void name::deletename()
{
if(pname)
{
delete pname;
pname=0;
}
}*/
void main()
{
name s("clau");
name t;
//t.display();
t=s;
t.display();
}
//去掉重载赋值运算符函数之后,结果怎么就不调用拷贝构造函数呢?