Most concise way to disable copying class in C++11

后端 未结 4 1048
日久生厌
日久生厌 2021-02-05 08:28

I have a problem dealing with deprecated since C++11 default generation of copy constructor and copy assignment operator when there is a user-defined destructor.

For mos

4条回答
  •  甜味超标
    2021-02-05 08:39

    With C++11, a clean way is to follow the pattern used in boost (see here)

    You basically create a base class where copy constructor and copy assignment are deleted, and inherit it:

    class non_copyable
    {
    protected:
        non_copyable() = default;
        ~non_copyable() = default;
    
        non_copyable(non_copyable const &) = delete;
        void operator=(non_copyable const &x) = delete;
    };
    
    class MyClass: public non_copyable
    {
    ...
    }
    

提交回复
热议问题