Chapter 7. Dynamic Memory Allocation
In the previous chapter, we talked about class definitions and how to devise your own custom class. We discussed how by devising our own custom classes, we can construct variables that represented entities within your game or program.
In this chapter, we will talk about dynamic memory allocations and how to create space in memory for groups of objects.
Assume that we have a simplified version of class Player
, as before, with only a constructor and a destructor:
class Player { string name; int hp; public: Player(){ cout << "Player born" << endl; } ~Player(){ cout << "Player died" << endl; } };
We talked earlier about the scope of a variable in C++; to recap, the scope of a variable is the section of the program where that variable can be used. The scope of a variable is generally inside the block in which it was declared. A block is just any section of code contained between { and }. Here is a...