In code which wrap a resource which should be freed once and only once, is it idiomatic to do something like the following to guarantee this? Is there a superior approach?
class SocketWrapper {
SocketWrapper() {
fd = socket(AF_INET, SOCK_STREAM, 0);
}
~SocketWrapper() {
if(fd < 0){
close(fd);
}
}
SocketWrapper(SocketWrapper &&other){
fd = other.fd;
other.fd = -1;
}
//similar move assignment
private:
int fd{-1};
};