< Previous | Contents | Next >

Questions and Answers

Q: Why should you use aggregation?

A: To create more complex objects from other objects. Q: What is composition?

A: A form of aggregation in which the composite object is responsible for the creation and destruction of its object parts. Composition is often called a uses arelationship.

Questions and Answers 327


Q: When should I use a friend function?

A: When you need a function to have access to the non-public members of a class.

Q: What is a friend member function?

A: A member function of one class that can access all of the members of another class.

Q: What is a friend class?

A: A class that can access all of the members of another class. Q: Cant operator overloading become confusing?

A: Yes. Giving too many meanings or unintuitive meanings to operators can lead to code thats difficult to understand.

Q: What happens when I instantiate a new object on the heap?

A: All of the data members will occupy memory on the heap and not on the stack.

Q: Can I access an object through a constant pointer?

A: Sure. But you can only access constant member functions through a constant pointer.

Q: Whats wrong with shallow copies?

A: Because shallow copies share references to the same chunks of memory, a change to one object will be reflected in another object.

Q: What is a linked list?

A: A dynamic data structure that consists of a sequence of linked nodes. Q: How is a linked list different from a vector?

A: Linked lists permit insertion and removal of nodes at any point in the list but do not allow random access, like vectors. However, the insertion and deletion of nodes in the middle of the list can be more efficient than the insertion and deletion of elements in the middle of vectors.

328 Chapter 9 n Advanced Classes and Dynamic Memory: Game Lobby


Q: Is there a container class from the STL that serves as a linked list? A: Yes, the list class.

Q: Is the data structure used in the Game Lobby program a linked list? A: It shares similarities to a linked list, but it is really a queue.

Q: Whats a queue?

A: A data structure in which elements are removed in the same order in which they were entered. This process is often called first in, first out (FIFO).

Q: Is there a kind of container from the STL that serves as a queue? A: Yes, the queue container adaptor.