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

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

    Deleting the copy-constructor and copy-assignment operator is the simplest and clearest way to disable copying:

    class X
    {
        X(X const &) = delete;
        void operator=(X const &x) = delete;
    };
    

    I don't follow what you are talking about with virtual destructors in the question body . It sounds like you're asking for a way to make your code take up fewer characters of source code, but also be more cryptic to anybody looking at it.

    If the list of deleted functions bothers you, you could hide them behind a macro, I guess.

     #define NON_COPYABLE_NOR_MOVABLE(T) \ 
          T(T const &) = delete; \
          void operator=(T const &t) = delete; \
          T(T &&) = delete;
    

提交回复
热议问题