dkfsjfvdkjvnsdkn
dkfsjfvdkjvnsdkn

Reputation: 11

C++ dynamic array

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

Upvotes: 1

Views: 874

Answers (2)

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272507

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 */));

Upvotes: 5

Sarfaraz Nawaz
Sarfaraz Nawaz

Reputation: 361442

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

Upvotes: 1

Related Questions