深拷贝与浅拷贝
#include <iostream>
using namespace std;
class Person
{
public:
Person()
{
cout << "无参构造函数调用。" << endl;
}
Person(int age,double hight)
{
m_Age = age;
m_Hight = new double(hight);
cout << "有参构造函数调用。" << endl;
}
Person(const Person& p)
{
m_Age = p.m_Age;
m_Hight = new double(*p.m_Hight);
cout << "拷贝构造函数调用。" << endl;
}
~Person()
{
cout << "析构函数调用。" << endl;
}
public:
int m_Age;
double *m_Hight;
};
void test01()
{
Person p1(18, 165.3);
Person p2(p1);
cout << "p1的年龄为:" << p1.m_Age << "\tp1的身高为:" << *p1.m_Hight << endl;
cout << "p2的年龄为:" << p2.m_Age << "\tp2的身高为:" << *p2.m_Hight << endl;
}
int main()
{
test01();
system("pause");
return 0;
}