I know how to create a array of dynamic objects.
For example, the class name is Stock.
Stock *stockArray[4];
for(int i = 0 ; i < 4;i++)
{
stockArray[i] = new Stock();
}
How do you change this to dynamic array of dynamic objects?
What I tried:
Stock stockArrayPointer = new Stock stock[4];
It doesn't work and the error is "The value of Stock** cannot be used to initalize an entity of type Stock.
Second question is after the creation of dynamic array of dynamic objects, what is the syntax to access the pointers in the array.
Now, I use stockArray[i] = new Stock(); How will this change?
Need some guidance on this...
I did something which worked perfectly:
Yes... I used pointer to pointer for the array part, and it worked perfectly for variable sized arrays.
The type of a variable to a dynamic array is a pointer to the first object of the array. You want an array of dynamically allocated Stock objects, so an array of pointers to Stock, so your variable is a pointer to a pointer to Stock:
and freeing it:
If you are using c++ then you shouldn't reinvent the wheel, just use vectors:
Edit:
I didn't understand your question, if you just want to have and array of arrays allocated on the heap just use:
works only if the Stock class has a zero argument constructor if it does not have any zero argument constructor you cannot create an array of dynamic objects dynamically
you can as said create a array of dynamic object with a static array like
but the syntax
or as said
use vectors...
vectors are memory allocated on heap
so the vector is a dynamic allocation
or
The reason why
does not hold is because this means you are using the new operator incorrectly