There are a few design points illustrating the use of RAII (Resource Acquisition Is Initialization)
TcpConnection
owns the connection file descriptor. It hold a unique pointer to a connectionSocket
. In the ctor ofTcpConnection
, it will create an RAII object ofSocket
. The file descriptor binded to thatSocket
is close by the dtor ofSocket
(by calling::close
). So whenTcpConnection
is destroyed, the dtor ofTcpConnection
doesn't need to do anything with theSocket
's file descriptor, because theSocket
object itself can manage it. The essence of RALL is resource lifetime management: you don't have to manually call new/delete to control resource usage.