c++ - protected destructor object allocation on heap vs stack -
if destructor protected, why allocating object on stack not allowed, allocating on heap allowed?
class foo { public: foo() { } protected: ~foo() { } }; int main() { foo* objonheap = new foo(); // compiles fine foo objonstack; // complains destructor protected return 0; }
when create object automatic storage duration (the standard term call "on stack"), implicitly destroyed when object goes out of scope. requires publicly accessible destructor. when allocate object dynamically new
, not happen. dynamically allocated object destroyed if explicitly (e.g. delete
). aren't trying that, don't error. error if did this:
delete objonheap;
Comments
Post a Comment