Sams Teach Yourself C++ in 21 Days

(singke) #1
Templates 713

19


Q When do I use general template friend classes?
A When every instance, regardless of type, should be a friend to this class or
function.
Q When do I use type-specific template friend classes or functions?
A When you want to establish a one-to-one relationship between two classes. For
example,array<int>should match iterator<int>, but not iterator<Animal>.

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 is the difference between a template and a macro?

  2. What is the difference between the parameter in a template and the parameter in a
    function?

  3. What is the difference between a type-specific template friend class and a general
    template friend class?

  4. Is it possible to provide special behavior for one instance of a template but not for
    other instances?

  5. How many static variables are created if you put one static member into a template
    class definition?

  6. What attributes must your class have to be used with the standard containers?

  7. What does STL stand for and why is the STL important?


Exercises ........................................................................................................



  1. Create a template based on this Listclass:
    class List
    {
    private:


public:
List():head(0),tail(0),theCount(0) {}
virtual ~List();
void insert( int value );
Free download pdf