Reference Class Members
- This is a little out of order, but it is useful here.
- I want to have a toss the ball game.
- You know, kids stand in a circle.
- They toss the ball to each other.
- Each can toss in a unique way.
- I built a hierarchy of tossing methods.
- I built an array of players.
- But I have a problem.
- My players need access to the array, at least for the array size.
- I might want to make a "pass to friend one of my friends" player and I would need the ability to find their friends.
- This is a problem I always have with board games.
- The board probably holds the pieces.
- But the pieces need access to the board.
- I really don't want to pass the board to all of the pieces all of the time.
- I could create a singleton, much like the message logger we discussed before.
- But I have another solution available as well.
- I will declare a reference data member
- Remember references
- On first assignment, a pointer is copied.
- This must be done at creation time.
- After this, the pointer is always dereferenced.
- This sounds like a place for a ctor-initializer.
- Take a look at PlayerT.h in the game code.