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

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -