I read a book about programming patterns in games, and decided I would give writing a small engine for fun/practice while I'm not taking classes, which makes a lot of use of object pointers.
I've created a Tile class to represent an area of the map (situated on a standard 2D coordinate plain, which is a 2D array of pointers to Tile objects) which has other types of objects (infamous tile traps, a character, and a ground type) as shown below, which holds pointers to the data the tile object will hold.
class Tile
{
public:
enum TileType{
WATER_DEEP,
WATER_SHALLOW,
GRASS,
MUD
};
Trap* getTileTrap();
GameObject* getTileObject();
TileType* getTileType();
Trap *tileTrap;
GameObject *tileObject;
TileType *tileType;
Tile();
virtual ~Tile();
protected:
private:
};
What I'm curious about is whether it is more efficient, memory/performance wise, to keep it how it is written, having this tile object (which is allocated on the heap) contain pointers to other objects, or should I scrap the pointers and simply return the objects?
If I keep it how it is written, are the objects the pointers point to created somewhere else on the heap? Will they be allocated somewhere random, or will they be allocated along with the space allotted to the Tile object when it is created?