dimanche 4 novembre 2018

RAII classes with transient resources

I'm fairly new to many modern C++/OOP concepts and I've got a question about RAII design.

Consider a class that manages and interfaces a transient resource: A TCP socket connection, a connection to a Bluetooth device, and communication with said devices. Things like this. We can assume the class is completely useless if the resource is or becomes unavailable.

I can't decide if RAII (specifically, acquiring/connecting to the resource in the ctor) is good or bad for this type of class.

On the one hand, if the class functionality depends on the resource, it seems to make sense to throw from the ctor rather than on some later Connect() call. On the other hand, acquiring a transient resource typically involves some blocking and/or async operation, which feels like poor constructor design to me.

I've found a few other topics that dance around this question, but haven't fully satisfied my curiosity: RAII, Berkeley Sockets, and STL Containers

RAII for resources that can be invalidated

Any guidance on best-practices will be appreciated!

Aucun commentaire:

Enregistrer un commentaire