< Previous | Contents | Next >

Advanced Classes and Dynamic Memory: Game Lobby


C++ gives a game programmer a high degree of control over the computer. One of the most fundamental abilities is direct control over memory. In this chapter, youll learn about dynamic memorymemory that you manage yourself. But with great power comes great responsibility, so youll also see the pitfalls of dynamic memory and how to avoid them. Youll learn a few more things about classes, too. Specifically, youll learn to:

n Combine objects

n Use friend functions

n Overload operators

n Dynamically allocate and free memory

n Avoid memory leaks

n Produce deep copies of objects


 

Using AggregationIntroducing the Critter Farm ProgramUsing Object Data MembersUsing Container Data MembersUsing Friend Functions and Operator OverloadingIntroducing the Friend Critter ProgramCreating Friend FunctionsOverloading OperatorsDynamically Allocating MemoryIntroducing the Heap ProgramUsing the new OperatorUsing the delete OperatorAvoiding Memory LeaksWorking with Data Members and the HeapIntroducing the Heap Data Member ProgramDeclaring Data Members that Point to Values on the HeapDeclaring and Defining DestructorsDeclaring and Defining Copy ConstructorsOverloading the Assignment OperatorIntroducing the Game Lobby ProgramThe Player ClassThe Lobby ClassThe Lobby::AddPlayer() Member FunctionThe Lobby::RemovePlayer() Member FunctionThe Lobby::Clear() Member FunctionThe operator<<() Member FunctionThe main() FunctionSummaryQuestions and AnswersDiscussion QuestionsExercises