With high risk of duplicate,
In C++, if you have custom destructor, you probably need to write own copy constructor and copy assignment operator.
In C++11 you also probably need to do move constructor and move assignment operator.
Why compiler auto-generates all these methods, if there is a custom destructor?
Note:
Question does not ask what are conditions of auto-generating all these methods.
Question is why is decided those methods to be auto-generated even d-tor
is added.
I can post several examples of broken code because of generating those methods, such this one:
class FileDescriptorGuard{
int fd;
public:
FileDescriptorGuard(int const fd) : fd(fd){}
~FileDescriptorGuard(){
::close(fd);
}
};
Here disaster will happen when object is copied or moved.