Why does the insertion of user defined destructor require an user defined copy constructor

后端 未结 2 399
走了就别回头了
走了就别回头了 2021-01-21 02:10

The following code compiles:

#include 
#include 
#include 

using namespace std;

class container
{
public:
    conta         


        
2条回答
  •  醉梦人生
    2021-01-21 03:04

    The idea is that, if the compiler-generated destructor is not good enough for your class, then chances are the copy constructor and copy assignment operator are also not good enough, so the compiler may delete the implicit implementations of those copy operations. Technically, the compiler may still give you implicit copy operations even if you have a user-defined destructor, but that behavior is deprecated in c++11.

    See Rule of Three

    AFAIK, you still need a copy constructor because buildShip() returns by value.

    (However, it's interesting that you are able to compile the versions that use an implicit copy constructor. You shouldn't be able to do that because of the unique_ptr member...)

提交回复
热议问题