C++(深拷贝与浅拷贝)

深拷贝与浅拷贝

  • 浅拷贝:简单的赋值拷贝操作
  • 深拷贝:在堆区重新申请空间,进行拷贝操作

示例:

#include <iostream>
using namespace std;

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.age;
            m_height=new int(*p.m_height);
      }

      //析构函数
      ~Person()
      {
            cout<<"析构函数!"<<endl;
            if(m_height!=NULL)
            {
                  delete m_height;
                  m_height=NULL;
            }
      }
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;
}

posted @ 2020-09-05 23:31  flyingswallow  阅读(168)  评论(0编辑  收藏  举报