devxlogo

The Order of Local Objects’ Destruction

The Order of Local Objects’ Destruction

On exit from a scope, destructors are called for all local automatic objects that are declared in that scope, in the reverse order of their declaration. For example:

   void f()  {    CmyClass obj1;  // 1    CmyClass obj2;  // 2   }

obj1 is constructed before obj2. On exit from f(), the objects are destroyed in the reverse order of their declaration, so obj2 will be destroyed before obj1.

devx-admin

Share the Post: