Sams Teach Yourself C++ in 21 Days

(singke) #1

Q&A ....................................................................................................................


Q Why are pointers so important?
A Pointers are important for a number of reasons. These include being able to use
pointers to hold the address of objects and to use them to pass arguments by refer-
ence. On Day 14, “Polymorphism,” you’ll see how pointers are used in class poly-
morphism. In addition, many operating systems and class libraries create objects
on your behalf and return pointers to them.
Q Why should I bother to declare anything on the free store?
A Objects on the free store persist after the return of a function. In addition, the capa-
bility to store objects on the free store enables you to decide at runtime how many
objects you need, instead of having to declare this in advance. This is explored in
greater depth tomorrow.
Q Why should I declare an object constif it limits what I can do with it?
A As a programmer, you want to enlist the compiler in helping you find bugs. One
serious bug that is difficult to find is a function that changes an object in ways that
aren’t obvious to the calling function. Declaring an object constprevents such
changes.

Workshop ............................................................................................................


The Workshop provides quiz questions to help you solidify your understanding of the
material covered and exercises to provide you with experience in using what you’ve
learned. Try to answer the quiz and exercise questions before checking the answers in
Appendix D, and be certain you understand the answers before continuing to tomorrow’s
lesson.

Quiz ................................................................................................................



  1. What operator is used to determine the address of a variable?

  2. What operator is used to find the value stored at an address held in a pointer?

  3. What is a pointer?

  4. What is the difference between the address stored in a pointer and the value at that
    address?


252 Day 8

Free download pdf