clone
function instead of copy construction/assignment深度拷贝 不采用拷贝构造 用clone代替,自定义的也不行。
浅拷贝:栈上的值拷贝
深拷贝:堆上对象拷贝
对于c++来说,一个类对象,可以存储在堆上,也可以存储在栈上。
对java来说,一个类对象,只存储在堆上,虽然没有指针但是处处是引用。
Copying a polymorphic class is discouraged due to the slicing problem, see C.67.
Clone
instead of copying in base classes 由于切片问题,不建议使用复制运算符操作多态类。
If you really need copy semantics, copy deeply:
Provide a virtual clone
function that will copy the actual most-derived type and return an owning pointer to the new object, 、、
and then in derived classes return the derived type (use a covariant return type).
class B {
public:
virtual owner<B*> clone() = 0;
virtual ~B() = default;
B(const B&) = delete;
B& operator=(const B&) = delete;
};
class D : public B {
public:
owner<D*> clone() override;
~D() override;
};
Generally, it is recommended to use smart pointers to represent ownership (see R.20).
However, because of language rules, the covariant return type cannot be a smart pointer:
通常,建议使用智能指针来表示所有权(请参阅[R.20] 但是,由于语言规则的原因,协变返回类型不能是智能指针
因为,智能指针不进行拷贝,而是move或者引用计数
https://www.thinbug.com/q/24334888
https://www.zhihu.com/question/52610176
https://www.codenong.com/16030081/
D::clone
can't return a unique_ptr<D>
//返回值是具体的类
while B::clone
returns unique_ptr<B>
.//返回值是抽象的类。
Therefore, you either need to consistently return unique_ptr<B>
in all overrides, or use owner<>
utility from the Guidelines Support Library.
防不胜防: unique_ptr 为了保证唯一是默认是禁止copy操作的,如果想需要采用 move拷贝,这个也是浅拷贝, 那么如何实现unique_ptr 智能指针的深度拷贝呢? 问题转化为一个类成员是指针指针,如何实现对一个类的深度拷贝
防不胜防: 既然你了解知道智能指针,他们直接区别是什么?具体来说使用场景区别 C++中的浅拷贝、深拷贝、智能指针
struct Base
{
//some stuff
auto clone() const { return std::unique_ptr<Base>(clone_impl()); }
protected:
virtual Base* clone_impl() const = 0;
};
struct Derived : public Base
{
//some stuff
protected:
virtual Derived* clone_impl() const override { return new Derived(*this); };
};
struct Foo
{
std::unique_ptr<Base> ptr; //points to Derived or some other derived class
//rule of five
~Foo() = default;
Foo(Foo const& other) : ptr(other.ptr->clone()) {} //原型设计模式
Foo(Foo && other) = default;
Foo& operator=(Foo const& other) { ptr = other.ptr->clone(); return *this; }
Foo& operator=(Foo && other) = default;
};
福利:【圣思园-张龙】深入理解JVM教程视频
赠人玫瑰,手留余香。 链接: https://pan.baidu.com/s/1WPkAOuaM9J-nbbMQtcakrQ 提取码: u83v