C++ 深拷贝与浅拷贝
浅拷贝:简单的赋值拷贝操作
深拷贝:在堆区重新申请空间,进行拷贝操作
示例:
class Person {
public:
Person() {
cout << "无参构造函数!" << endl;
}
Person(int age ,int height) {
cout << "有参构造函数!" << endl;
m_age = age;
m_height = new int(height);
}
Person(const Person& p) {
cout << "拷贝构造函数!" << endl;
m_age = p.m_age;
m_height = new int(*p.m_height);
}
~Person() {
cout << "析构函数!" << endl;
if (m_height != NULL)
{
delete m_height;
}
}
public:
int m_age;
int* m_height;
};
void test01()
{
Person p1(18, 180);
Person p2(p1);
cout << "p1的年龄: " << p1.m_age << " 身高: " << *p1.m_height << endl;
cout << "p2的年龄: " << p2.m_age << " 身高: " << *p2.m_height << endl;
}
int main() {
test01();
system("pause");
return 0;
}
输出:
有参构造函数!
拷贝构造函数!
p1的年龄: 18 身高: 180
p2的年龄: 18 身高: 180
析构函数!
析构函数!
总结:如果属性有在堆区开辟的,一定要自己提供拷贝构造函数,防止浅拷贝带来的问题
|