0 votes
in C Plus Plus by
What's the order in which the local objects are destructed?

1 Answer

0 votes
by

Consider following a piece of code:

Class A{

 ….

 };

 int main()

 {

 A a;

 A b;

 ...

 }

In the main function, we have two objects created one after the other. They are created in order, first a then b. But when these objects are deleted or if they go out of the scope, the destructor for each will be called in the reverse order in which they were constructed.

Hence, the destructor of b will be called first followed by a. Even if we have an array of objects, they will be destructed in the same way in the reverse order of their creation.

Related questions

0 votes
asked Jul 30, 2021 in Ansible by DavidAnderson
0 votes
asked Jan 11 in C Plus Plus by GeorgeBell
...