【问题标题】:What is the difference between a deep copy and a shallow copy?深拷贝和浅拷贝有什么区别?
【发布时间】:2010-09-16 03:07:09
【问题描述】:

深拷贝和浅拷贝有什么区别?

【问题讨论】:

    标签: language-agnostic copy deep-copy shallow-copy


    【解决方案1】:

    除了上述所有定义之外,还有一种最常用的深拷贝,在类的拷贝构造函数(或重载赋值运算符)中。

    浅拷贝 --> 是当你不提供拷贝构造函数时。在这里,只有对象被复制,而不是类的所有成员都被复制。

    深拷贝 --> 当你决定在你的类中实现复制构造函数或重载赋值并允许复制类的所有成员时。

    MyClass& MyClass(const MyClass& obj) // copy constructor for MyClass
    {
              // write your code, to copy all the members and return the new object
    }
    MyClass& operator=(const MyClass& obj) // overloading assignment operator,
    {
              // write your code, to copy all the members and return the new object
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-07
      • 2012-04-12
      • 2013-06-19
      • 2023-04-05
      • 2015-01-13
      • 1970-01-01
      相关资源
      最近更新 更多