Quote:
These are all invalid syntax in standard C++, in the last one the types don't match.
@Raeldor if your making arrays of pointers just to get round the limitations of C-style dynamic arrays using operators new i.e. your type does not have a default constructor or you want to allocate memory first then incrementally construct/initialize.
Doing it with an arrays of pointers is not an efficient method. What you really want is a dynamic array of the actual type but separate allocation/de-allocation & construction/destruction you can do this in C++ with operators new/delete but its very low-level and advance C++, potentially prone to error.
This is one of the reasons why we have std::vector it essentially takes care of all this for you among other things, guaranteed contiguousness of elements.
In both cases this can be done for multidimensional arrays too.
Just to recap, my code now looks like...
// create tiles
pTiles=new Tile*[nTilesAcross*nTilesDown];
for (int y=0; y < nTilesDown; y++)
for (int x=0; x < nTilesAcross; x++)
pTiles[y*nTilesAcross+x]=new Tile(nTileSize, x*nTileSize, y*nTileSize);
to initialize and...
// release tiles
for (int y=0; y < nTilesDown; y++)
for (int x=0; x < nTilesAcross; x++)
{
if (pTiles[y*nTilesAcross+x] != NULL)
delete pTiles[y*nTilesAcross+x];
}
// delete pointer array
delete [] pTiles;
Is this really bad c++? What additional benefit would std::vector give me considering I only want to index into these tiles using x/y values?
Thanks