0

I wrote the following code:

#include <iostream>

struct A
{
    int a;
    A(int){ std::cout << "A(int)" << std::endl; }
    A(){ std::cout << "A()" << std::endl; }
    ~A(){ std::cout << "~A()" << std::endl; }
};

int main()
{
    A *a = new A[100];
    delete[] a;
}

But I wanted to call A(2) while constructing the array, not A(). I tried this: new (2) A[100];, but the argument 2 is passed to the operator new[]. new A(2)[100]; doesn't work either.

stella
  • 2,546
  • 2
  • 18
  • 33
  • 1
    `A *a = new A[100]{2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2};` – M.M Oct 01 '15 at 03:49
  • Why not use a `std::vector`? – CinCout Oct 01 '15 at 03:56
  • @HappyCoder No reason, I just wanted to understand how arrays work. – stella Oct 01 '15 at 03:56

0 Answers0