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

前端 未结 6 2075
情话喂你
情话喂你 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:18

    Personally I think you've answered your own question, and should use the first approach.

    If you don't want it to be copyable at all, as you said it will throw a linker error. However if you use the second approach and you DO end up using the copy constructor by accident, it WILL compile and it WILL run; and you'll have absolutely no indication of where the inconsistency came from until you bust open a debugger. Or as sehe said, if you can use a modern compiler, use C++11's '= delete' notation.

提交回复
热议问题