How to approach copying objects with smart pointers as class attributes?

后端 未结 5 618
小鲜肉
小鲜肉 2021-01-02 16:15

From the boost library documentation I read this:

Conceptually, smart pointers are seen as owning the object pointed to, and thus responsible for de

5条回答
  •  -上瘾入骨i
    2021-01-02 16:27

    It's a bit late, but for future viewers: There's a ready-to-use implementation in my header-only library Aurora and its SmartPtr tutorial. With Aurora, it's trivial to implement deep-copy through smart pointers. The following code works for any copyable type T:

    aurora::CopiedPtr first(new T);
    aurora::CopiedPtr second = first; // deep copy
    

    This makes it often unnecessary to implement The Big Three/Five if your classes have pointer members.

提交回复
热议问题