Creation of Dynamic Array of Dynamic Objects in C++
arrays, c++, dynamic-memory-allocation
Solution
If you are using c++ then you shouldn't reinvent the wheel, just use vectors:
#include <vector>
std::vector< std::vector< Stock > > StockVector;
// do this as many times as you wish
StockVector.push_back( std::vector< Stock >() );
// Now you are adding a stock to the i-th stockarray
StockVector[i].push_back( Stock() );
Edit:
I didn't understand your question, if you just want to have and array of arrays allocated on the heap just use:
Stock** StockArrayArray = new Stock*[n]; // where n is number of arrays to create
for( int i = 0; i < n; ++i )
{
StockArrayArray[i] = new Stock[25];
}
// for freeing
for( int i = 0; i < n; ++i )
{
delete[] StockArrayArray[i];
}
delete[] StockArrayArray;
Problem
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...