关于“&”的使用问题
请大家看一下下面这段代码# include <iostream>
# include <string>
using namespace std;
class myclass
{
char *p;
public:
myclass (char *pn)
{
p = new char[strlen(pn)+1]; // 请问,这个地方为什么需要 +1 ?
strcpy(p,pn);
}
~myclass() { delete p; }
myclass &operator = (myclass &s) // 这里是重载赋值运算符,那么蓝色的2个 & 分别起什么作用啊?
{
delete p;
p = new char[strlen(s.p)+1];
strcpy(p,s.p);
return *this;
}
void display() {cout<<p<<endl;}
}
void main()
{
myclass s1("first object");
myclass s2("second object");
s2 = s1;
cout<<"s1 *p=";
s1.display();
cout<<"s2 *p=";
s2.display();
}