What's the most reliable way to prohibit a copy constructor in C++?

前端 未结 6 2055
情话喂你
情话喂你 2020-11-28 10:43

Sometimes it\'s necessary to prohibit a copy constructor in a C++ class so that class becomes \"non-copyable\". Of course, operator= should be prohibited at the

6条回答
  •  失恋的感觉
    2020-11-28 11:16

    The first method is how Boost solves it (source code), as far as I know, there's no drawbacks. In fact, the linker errors are the big advantage of that method. You want the errors to be at link time, not when your client is executing your code and it suddenly crashes.

    In case you are using Boost, you can save yourself some typing. This does the same as your first example:

    #include 
    
    class Class : boost::noncopyable {
    // Stuff here
    }
    

提交回复
热议问题