Rule of Three in C++

后端 未结 3 1344
一生所求
一生所求 2020-12-07 03:43

I\'ve read that The Rule of Three, What is The Rule of Three? is summarized as follows:

If you need to explicitly declare either the destructor, copy

3条回答
  •  一个人的身影
    2020-12-07 04:15

    In almost all cases, the compiler will generate these methods for you and you don't need to do anything. But, if implicitly generated copy constructor/assignment operator won't do what you want, and design-wise your class makes sense to be able to be copied, you should explicitly provide a copy ctor and assignment operator whether you use them both or not (as good practice).

    If, design-wise, your class makes sense to be noncopyable, you can declare but not define the copy ctor/assignment op.

提交回复
热议问题