1

I would like to make a dynamic array of a class with a constructor that has paramaters.

Where does the constructor's size parameter go?

ex. twoDArrayInDisguise = new dynamicArray(size)*[size];

Does not work

  • http://stackoverflow.com/questions/3016772/dynamic-memory-allocation-with-default-values This is similar question – ckv Jan 19 '11 at 12:45

2 Answers2

5

You cannot do this directly (when using new[], the default constructor is used).

Instead, use a std::vector. You can initialise each element in terms of a reference object, e.g.:

std::vector<T> vec(size, T(/* args */));
Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
  • +1 Also, this is the reason why T should support copy constructor and copy assignment if you intend to use it with a std::vector (or any other std container). – MatiasFG Jan 19 '11 at 13:38
1

In C++, you cannot dynamically create an array of a class with a constructor that has paramaters!

Nawaz
  • 353,942
  • 115
  • 666
  • 851