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){
close(fd);
}
}
SocketWrapper(SocketWrapper &&other){
fd = other.fd;
other.fd = 0;
}
//similar move assignment
private:
int fd{0};
};
Aucun commentaire:
Enregistrer un commentaire