I have noticed that many Poco classes have a protected destructor. This makes them more annoying to code with. For example here is some of my code:
struct W2: Poco::Util::WinRegistryConfiguration
{
typedef Poco::Util::WinRegistryConfiguration inherited;
using inherited::inherited;
};
std::string get_documents_folder()
{
W2 regc { "HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders" };
return regc.getString("Personal", "");
}
Of course, it would be much simpler if I could do away with W2
and just make regc
have type WinRegistryConfiguration
. But that is not possible because of the protected destructor.
I realize it is possible to use Poco::AutoPtr
instead , but then resource is wasted by doing a dynamic allocation with new
when automatic allocation should work fine.
My question is: what is the rationale for this and am I overlooking anything?
The reason is that WinRegistryConfiguration is reference-counted (inheriting from Poco::RefCountedObject). The protected destructor is meant to prevent clients from instantiating the class on the stack, or directly deleting the object. Instead, you should instantiate the class via new
, and manage the lifetime through the RefCountedObject methods.
I'm not familiar with Poco, but there should also be a smart pointer class that manages reference-counted objects by automatically calling the RefCountedObject methods.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With