Notes on Templates and Friends

We have seen that functions and entire classes can be declared as friends of nontemplate classes. With class templates, friendship can be established between a class template and a global function, a member function of another class (possibly a class-template specialization), or even an entire class (possibly a class-template specialization).

Throughout this section, we assume that we have defined a class template for a class named X with a single type parameter T, as in:

template< typename T > class X

Under this assumption, it is possible to make a function f1 a friend of every class-template specialization instantiated from the class template for class X. To do so, use a friendship declaration of the form


friend void f1();

For example, function f1 is a friend of X< double >, X< string > and X< Employee >, etc.

It is also possible to make a function f2 a friend of only a class-template specialization with the same type argument. To do so, use a friendship declaration of the form

friend void f2( X< T > & );

For example, if T is a float, function f2( X< float > & ) is a friend of class-template specialization X< float > but not a friend of class-template specification X< string >.

You can declare that a member function of another class is a friend of any class-template specialization generated from the class template. To do so, the friend declaration must qualify the name of the other class's member function using the class name and the binary scope resolution operator, as in:

friend void A::f3();

The declaration makes member function f3 of class A a friend of every class-template specialization instantiated from the preceding class template. For example, function f3 of class A is a friend of X< double >, X< string > and X< Employee >, etc.

As with a global function, another class's member function can be a friend of only a class-template specialization with the same type argument. A friendship declaration of the form

friend void C< T >::f4( X< T > & );

for a particular type T such as float makes member function

C< float >::f4( X< float > & )

a friend function of only class-template specialization X< float >.

In some cases, it is desirable to make an entire class's set of member functions friends of a class template. In this case, a friend declaration of the form

friend class Y;

makes every member function of class Y a friend of every class-template specialization produced from the class template X.

Finally, it is possible to make all member functions of one class-template specialization friends of another class-template specialization with the same type argument. For example, a friend declaration of the form:

friend class Z< T >;

indicates that when a class-template specialization is instantiated with a particular type for T (such as float), all members of class Z< float > become friends of class-template specialization X< float >. We use this particular relationship in several examples of Chapter 21, Data Structures.

Introduction to Computers, the Internet and World Wide Web

Introduction to C++ Programming

Introduction to Classes and Objects

Control Statements: Part 1

Control Statements: Part 2

Functions and an Introduction to Recursion

Arrays and Vectors

Pointers and Pointer-Based Strings

Classes: A Deeper Look, Part 1

Classes: A Deeper Look, Part 2

Operator Overloading; String and Array Objects

Object-Oriented Programming: Inheritance

Object-Oriented Programming: Polymorphism

Templates

Stream Input/Output

Exception Handling

File Processing

Class string and String Stream Processing

Web Programming

Searching and Sorting

Data Structures

Bits, Characters, C-Strings and structs

Standard Template Library (STL)

Other Topics

Appendix A. Operator Precedence and Associativity Chart

Appendix B. ASCII Character Set

Appendix C. Fundamental Types

Appendix D. Number Systems

Appendix E. C Legacy Code Topics

Appendix F. Preprocessor

Appendix G. ATM Case Study Code

Appendix H. UML 2: Additional Diagram Types

Appendix I. C++ Internet and Web Resources

Appendix J. Introduction to XHTML

Appendix K. XHTML Special Characters

Appendix L. Using the Visual Studio .NET Debugger

Appendix M. Using the GNU C++ Debugger

Bibliography



C++ How to Program
C++ How to Program (5th Edition)
ISBN: 0131857576
EAN: 2147483647
Year: 2004
Pages: 627

Similar book on Amazon

Flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net