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;
}
相关推荐
程序员老陆25 分钟前
深入理解 C++ thread_local:线程私有存储的正确打开方式
开发语言·c++·程序设计
半杯咖啡半行码4 小时前
C++ 编程与 STL 模板:从泛型编程到内存安全详解
开发语言·c++
xh didida5 小时前
Linux -- 基础IO
linux·服务器·开发语言·c++
垆边人似月.5 小时前
华为机试题 :两数之和
c++·算法·华为
个 人 练 习 生5 小时前
C++ string 类模拟实现:从底层理解字符串(上)
开发语言·c++·经验分享·学习·程序人生
垆边人似月.5 小时前
华为机试题 :有效的括号
c++·算法·华为
坤坤子吖5 小时前
C++智能指针:RAII、shared_ptr与内存泄漏
开发语言·c++·笔记·学习
神仙别闹6 小时前
基于C++实现的贪吃蛇游戏平台
java·c++·游戏
王老师青少年编程6 小时前
2026年6月GESP真题及题解(C++三级): 选择题和判断题(题解)
c++·真题·gesp·答案·csp·三级·信奥赛
C++ 老炮儿的技术栈6 小时前
static的作用
c语言·c++·人工智能·单片机·c