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

后端 未结 4 1001
日久生厌
日久生厌 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:33

    You can do it by this(which is used by Caffe: a fast open framework for deep learning):

    // Disable the copy and assignment operator for a class.
    #define DISABLE_COPY_AND_ASSIGN(classname) \
    private:\
      classname(const classname&);\
      classname& operator=(const classname&)
    

    Usage example:

    class CNoCopyable{
    
        public:
            CNoCopyable(int i):m_d(i){}
    
        private:
            int m_d;
            // add this line(pass class name)
            DISABLE_COPY_AND_ASSIGN(CNoCopyable);
    
    };
    

提交回复
热议问题