fork download
  1. #include <iostream>
  2. #include <new>
  3.  
  4. class A {
  5. public:
  6. int x;
  7.  
  8. public:
  9. A(int x = 0) : x(x) {
  10. std::cout << "In " << __func__ << ", x = " << x << std::endl;
  11. }
  12. };
  13.  
  14. int main(int argc, char* argv[]) {
  15. const int N = 10;
  16. A* array = new A[N];
  17.  
  18. for (int i = 0; i < N; ++ i) {
  19. new (array + i) A(i);
  20. }
  21.  
  22. for (int i = 0; i < N; ++ i) {
  23. std::cout << array[i].x << std::endl;
  24. }
  25. return 0;
  26. }
  27.  
Success #stdin #stdout 0s 2984KB
stdin
Standard input is empty
stdout
In A, x = 0
In A, x = 0
In A, x = 0
In A, x = 0
In A, x = 0
In A, x = 0
In A, x = 0
In A, x = 0
In A, x = 0
In A, x = 0
In A, x = 0
In A, x = 1
In A, x = 2
In A, x = 3
In A, x = 4
In A, x = 5
In A, x = 6
In A, x = 7
In A, x = 8
In A, x = 9
0
1
2
3
4
5
6
7
8
9