Macros to disallow class copy and assignment. Google -vs- Qt

前端 未结 11 1267
伪装坚强ぢ
伪装坚强ぢ 2020-12-13 06:22

To disallow copying or assigning a class it\'s common practice to make the copy constructor and assignment operator private. Both Google and Qt have macros to make this eas

11条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-13 07:02

    In practice I would say that both should not be used anymore if you have a C++11 compiler.

    You should instead use the delete feature , see here

    Meaning of = delete after function declaration

    and here

    http://www.stroustrup.com/C++11FAQ.html#default

    Why : essentially because compiler message is much more clearer. When the compiler need one of the copy or copy assignment operator, it immediately points out to the line where the =delete was coded.

    Better and complete explanations can also be found in Item 11: Prefer deleted functions to private undefined ones from Effective Modern C++ book by Scott Meyers

提交回复
热议问题