According to the C++23 standard, what is the output of this program?
#include <iostream>
class show_id
{
public:
~show_id() { std::cout << id; }
int id;
};
int main()
{
delete[] new show_id[3]{ {0}, {1}, {2} };
}In what order do C++ objects tend to be destroyed?
210
The core of the question is "in what order are the objects destroyed?". In C++, objects are generally destroyed in the reverse order that they were constructed. delete[] is no exception.
In the case of an array, the elements will be destroyed in order of decreasing address (that is, in reverse order of the completion of their constructor; see §[class.base.init]).
So the objects are destroyed in the order 2, 1, 0, and 210 is printed.