How to use a object whose copy constructor and copy assignment is private?

前端 未结 6 576
甜味超标
甜味超标 2021-01-14 07:43

In reading TCPL, I got a problem, as the title refered, and then \'private\' class is:

class Unique_handle {
private:
    Unique_handle& operato         


        
6条回答
  •  生来不讨喜
    2021-01-14 08:29

    You can use a shared_ptr to share the object:

    class Y
    {
    public:
      Y(): mHandle(new UniqueHandle()) {}
    
    private:
      boost::shared_ptr mHandle;
    };
    

    It's as simple as that.

    If you don't want shared ownership, you can use boost::scoped_ptr or the newly created std::unique_ptr if you have access to it, and then implement the CopyConstructor and AssignmentOperator yourself, taking care of their semantics.

提交回复
热议问题