C++ 中的深浅拷贝

浅拷贝是逐字节拷贝对象的成员变量 ,这里如果成员变量是指针,很明显,只拷贝了指针储存的地址并没有去拷贝指针指向的值。由此引出了C++中的深浅拷贝。拷贝赋值与拷贝构造类似,这里以拷贝构造为例来说明深浅拷贝。

首先,这里我们有一个类,先不写拷贝构造函数:

cpp 复制代码
#include <iostream>
#include <vector>

class TestClass {
public:
  TestClass(int num)
    : num_{ 1 },
    num_ptr_{ new int(num) }
  {
    std::cout << "default constructor\n";
  }

  ~TestClass() {
    num_ptr_ = nullptr;
    std::cout << "destructor\n";
  }

private:
  int num_;
  int* num_ptr_;
};

int main() {
  int num{ 1 };
  TestClass test_default{ num }; // 默认构造
  TestClass test_move{ test_default };  // 拷贝构造

  std::cout << "Program finished!\n";
  return 0;
}

一个类如果不显示写拷贝构造函数,那么编译器会自动为这个类对象创建一个拷贝构造函数,默认行为是逐字节拷贝对象成员变量,我们看调试结果:

两个对象中num_ptr_地址相同,指向的值相同,这就是浅拷贝。

接着我们补充拷贝构造函数:

cpp 复制代码
// 拷贝构造
TestClass(const TestClass& other)
  : num_{ other.num_ },
  num_ptr_{ new int(*other.num_ptr_) }
{
  std::cout << "copy constructor\n";
}

然后看调试结果:

两个对象中num_ptr_地址不同,指向的值相同,这就是深拷贝。深拷贝确保了两个对象没有共享同一份数据

写了拷贝构造函数以后,编译器不再自动生成,而是调用我们写的拷贝构造函数,具体是深拷贝还是浅拷贝取决于我们写的这个拷贝构造函数。如果我们在这个拷贝构造函数中做了浅拷贝,那么实际上最终还是浅拷贝。

最后贴出完整代码:

cpp 复制代码
#include <iostream>
#include <vector>

class TestClass {
public:
  TestClass(int num)
    : num_{ 1 },
    num_ptr_{ new int(num) }
  {
    std::cout << "default constructor\n";
  }

  // 拷贝构造
  TestClass(const TestClass& other)
    : num_{ other.num_ },
    num_ptr_{ new int(*other.num_ptr_) }
  {
    std::cout << "copy constructor\n";
  }

  ~TestClass() {
    num_ptr_ = nullptr;
    std::cout << "destructor\n";
  }

private:
  int num_;
  int* num_ptr_;
};

int main() {
  int num{ 1 };
  TestClass test_default{ num }; // 默认构造
  TestClass test_move{ test_default };  // 拷贝构造

  std::cout << "Program finished!\n";
  return 0;
}
相关推荐
汉克老师7 小时前
GESP2025年6月认证C++五级( 第一部分选择题(9-15))
c++·贪心算法·分治算法·二分算法·gesp5级·gesp五级·高精度除法
qiqsevenqiqiqiqi7 小时前
MT2048三连 暴力→数学推导→O (n) 优化
数据结构·c++·算法
ximu_polaris7 小时前
设计模式(C++)-行为型模式-模版方法模式
c++·设计模式
码之气三段.7 小时前
十五届山东ccpc省赛补题(update)
数据结构·c++·算法
王老师青少年编程8 小时前
csp信奥赛C++高频考点专项训练之贪心算法 --【跳跃与过河问题】:过河问题
c++·算法·贪心·csp·信奥赛·跳跃与过河问题·过河问题
是个西兰花8 小时前
C++11:智能指针
开发语言·c++·智能指针·rall
CN-Dust8 小时前
【C++专题】输出cout例题
开发语言·c++
沉默-_-9 小时前
备战蓝桥杯-哈希
c++·学习·算法·蓝桥杯·哈希算法
Reese_Cool9 小时前
【STL】蓝桥杯/天梯赛终极杀器!10个C++字符串核心技巧,暴力破解高频考点
开发语言·c++·蓝桥杯·stl
hehelm9 小时前
C++ 模拟实现 AVL 树
开发语言·c++