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!
Dynamic initialization of object refers to initializing the objects at a run time i.e., the initial value of an object is provided during run time. It can be achieved by using constructors and by passing parameters to the constructors.
To pass an array to a constructor we need to pass in the array variable to the constructor while creating an object.
Dynamic constructor is used to allocate the memory to the objects at the run time. Memory is allocated at run time with the help of 'new' operator. By using this constructor, we can dynamically initialize the objects.
When allocation of memory is done dynamically using dynamic memory allocator new in a constructor, it is known as dynamic constructor. By using this, we can dynamically initialize the objects.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With