how to dynamically declare an array of objects with a constructor in c++

I was wondering if it was possible to create an array of objects when the object needs things passed into it for the constructor. I want something like this:

MyClass *myVar;
myVar = new MyClass[num];  // I would like to specify the array size after declaration
int i = 0;
for(i = 0;i < num;i++)
   myVar[i] = new MyClass(0,0);  // I would also like to populate the array with new objects

I know that this works:

MyClass *myVar;
myVar = new MyClass[num];

but this only works when the constructor has nothing passed into it. Is what I am trying to do possible? If so, how do I do it?

EDIT: I found out how to do it with using arrays. Here is how I did it:

MyClass **myVar;
myVar = new MyClass *[num];
for(i = 0;i < num;i++)
   myVar[0] = new MyClass(0,0);

I would use vectors and such but my teacher has told us to use basic arrays whenever possible. The above solution I actually got from some code my teacher wrote. Thank you all for your help!


MyClass *myVar;
myVar = new MyClass[num];

Actually in this form you cannot invoke constructor which takes parameter(s). It is not allowed by the language specification.

However, if you use std::vector, which I recommend you to use, then you can create a vector calling non-default constructor as:

#include <vector> //header file where std::vector is defined

std::vector<MyClass>  arr(num, MyClass(10,20));

It creates a vector of num elements, each element is created by calling copy-constructor of the class, passing MyClass(10,20) as argument to it.

The vector is also good because now you dont need to manage memory yourself. Neither manual allocation, nor manual deallocation. Plus, you can know the number of elements by calling arr.size() anytime. You always know how many elements the vector contains. You can also add elements anytime, just by calling .push_back() member function as:

arr.push_back(MyClass(20,30)); 

And now you can access elements, just like you access array, i.e by using index:

f(arr[i]); // 0 <= i < arr.size();

Additionally, you can use iterators which facilitate idiomatic programming, enabling you to use various algorithmic functions from <algorithm> header as:

#include <algorithm> //header file where std::for_each is defined

std::for_each(arr.begin(), arr.end(), f);

where f is function which takes one argument of type MyClass& (or MyClass const &) depending on what you want to do in f.

In C++11, you can use lambda as:

std::for_each(arr.begin(), arr.end(), [](const MyClass & m)
                                      {
                                           //working with m 
                                      });

In C++0x, this grammar works, which can call the non-default constructor in new expression:

MyClass *myVar;
myVar = new MyClass[2]{{10, 20},{20, 30}};

But I doubt if it works when the number of elements in available only at run time.

The vector approach would be better, as shown in Nawaz's answer.


Pointer to pointer is equivalent to 1. array of pointers, and 2. vector<T*> vector of pointers. One way I've done this in the past is using a double pointer. This approach eliminates the overhead of vector data structure and preferred memory efficient is needed.

MyClass ** myvar;
myvar = new Myclass*[num]
for(int i = 0; i < num; i++){
*(myvar+i) = new Myclass(i);}

Works with pretty much any control structure you can imagine. The drawback is that the allocation of memory is not contiguous and my affect speed for large number of num.


You can do something like this too:

MyClass *myVar[num];

for(int i = 0; i < num; i += 1)
{
    myVar[i] = new MyClass(0, 0);
}