Elements of a C ++ array

Is there any guarantee in C ++ about the order in which array elements are built?

#include <iostream> using namespace std; struct A { A() { cout << this << endl; } }; int main() { cout << "[0] is " << new A[3]; } 

displays

 0x602010 0x602011 0x602012 [0] is 0x602010 

implying that the elements were built in the sequence [0], [1] and [2]. Is this order guaranteed by language?

+6
source share
1 answer

Yes, this is guaranteed by C ++ 11 12.6 / 3 ([class.init] / 3):

When an array of class objects is initialized (explicitly or implicitly), and the elements are initialized by the constructor, the constructor should be called for each element of the array , following the order of the substring

+11
source

Source: https://habr.com/ru/post/954680/


All Articles