I understand the basics of pointers and references, but the biggest issue I have is in deciding when they should be used (and which). The main example I will give is one of a basic game. Assume the setup is something like:
- new World
- Camera
- Map
World is a pointer because every time the game starts a new save or loads an existing one, it deletes world and loads a new one. But inside of World, only one existence of Camera and Map should ever exist and only for the duration of World. If World is destroyed, then obviously they should be as well. However.. lets say Map needs to access camera (but so do other objects), should Camera be passed by reference into Map or as a pointer, or? For instance, if its supposed to be by reference, should it be:
map = Map(&camera);
(inside map class) Map(Camera camera) {...}
Or more like:
map = Map(camera);
(inside map class) Map(Camera &camera) {...}
Also, lets say Map holds a 2D vector of tiles called grid. Something like:
std::vector< std::vector< Tile > > > grid;
Now lets say I have a PathFinder class that needs that grid passed in. It needs to edit the tiles directly to change f, g, etc values (for pathfinding). Should that 2D vector be just a normal 2D vector of Tile's and the entire thing passed by reference to the PathFinder? Or should it be a 2D vector of Tile pointers?
Also, NPC's and Player will have a currentTile which is the tile they currently are on. They will need to have a reference or pointer to that tile so they can also set themselves as an occupant on that tile via something like this inside the NPC/Player classes:
currentTile = tile;
currentTile->SetOccupant(this);
The other concern comes in when I then destroy that grid to load a new map, how do I easily handle making sure nothing is pointing to Tiles that no longer exist. Do I simply have to loop over those classes and set the currentTile to NULL?
This is where I started to really get confused by this stuff. Any help is appreciated as I'm obviously pretty nooby. @_@; And sorry if this isn't really game topic related. If it needs moved to a different stackexchange, just let me know or move it (if you can). >_<