浅拷贝

深拷贝

class Person
{
public:
Person()
{
cout << "无参构造函数!" << endl;
}
Person(int age, int heigth)
{
cout << "有参构造函数!" << endl;
m_age = age;
m_heigth = new int(heigth);
}
Person(const Person& p)
{
cout << "拷贝构造函数!" << endl;
m_age = p.m_age;
m_heigth = new int(*p.m_heigth);
}
~Person()
{
cout << "析构函数!" << endl;
if (m_heigth != NULL)
{
delete m_heigth;
m_heigth = NULL;
}
}
public:
int m_age;
int* m_heigth;
};
void test01()
{
Person p1(18, 180);
Person p2(p1);
cout << "p1的年龄: " << p1.m_age << " 身高: " << *p1.m_heigth << endl;
cout << "p2的年龄: " << p2.m_age << " 身高: " << *p2.m_heigth << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
