Powered by |
Why do I need a protected destructor?A protected destructor is used to prohibit from the memory allocation on stack or in the static data segment and from the explicit deallocation by Practical use of the protected destructor (usually it's also virtual):
class TestClass
{
public:
TestClass() {}
void Release()
{
//whatever required
delete this;
}
protected:
virtual ~TestClass() {}
};
int main(int argc, char** argv[])
{
// TestClass tc; // gives error C2248: 'TestClass::~TestClass' : cannot access
// protected member declared in class 'TestClass'
TestClass* pTC = new TestClass;
// delete pTC; // gives the same error
pTC->Release();
return 0;
}
|