下面代码正常运行
#include
using namespace std;
class Person
{
public:
Person(int age=0)
{
m_age = new int(age);
}
~Person()
{
if (m_age != NULL)
{
delete m_age;
m_age = NULL;
}
}
friend ostream& operator<<(ostream& output, Person &p);
Person& operator=(Person& p);
private:
int* m_age;
};
ostream& operator<<(ostream& output, Person &p)
{
output << *p.m_age;
return output;
}
Person& Person:: operator=(Person& p)
{
if (m_age != NULL)
{
delete m_age;
m_age = NULL;
}
m_age = new int(*p.m_age);
return *this;
}
void test01()
{
Person p1(19), p2(20);
p2 = p1;
cout << "p1=" << p1 << endl;
cout << "p2=" << p2 << endl;
}
int main()
{
test01();
}
可如果把输出流重载函数里第二个参数里的&去掉,代码就会出错
能给我解释一下为什么吗🙏