Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rationale for protected destructor

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?

like image 825
M.M Avatar asked Nov 09 '22 08:11

M.M


1 Answers

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.

like image 134
user1610015 Avatar answered Nov 15 '22 06:11

user1610015