C++ Quiz135 of 173
Difficulty:
⬅️ Prev: 281Quiz ID: 283Next: 284 ➡️

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} };
}
Hint

In what order do C++ objects tend to be destroyed?

Reveal answer

210

Explanation

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.

§[expr.delete]¶6:

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.

⬅️ Prev: 281Quiz ID: 283Next: 284 ➡️