6.6 FriendsA friend is permitted full access to private and protected members. A friend can be a function, function template, or member function, or a class or class template, in which case the entire class and all of its members are friends. Use the friend specifier to declare a friend in the class granting friendship. Note that friendship is given, not taken. In other words, if class A contains the declaration friend class B;, class B can access the private members of A, but A has no special privileges to access B (unless B declares A as a friend). By convention, the friend specifier is usually first, although it can appear in any order with other function and type specifiers. The friend declaration can appear anywhere in the class; the access level is not relevant. You cannot use a storage class specifier in a friend declaration. Instead, you should declare the function before the class definition (with the storage class, but without the friend specifier), then redeclare the function in the class definition (with the friend specifier and without the storage class). The function retains its original linkage. If the friend declaration is the first declaration of a function, the function gets external linkage. (See Chapter 2 for more information about storage classes and linkage.) For example: class demo; static void func(demo& d); class demo { friend void func(demo&); ... Friendship is not transitive—that is, the friend of my friend is not my friend (unless I declare so in a separate friend declaration)—nor is a nested class a friend just because the outer class is a friend. (See the next section, Section 6.7, for more information.) Friendship is not inherited. If a base class is a friend, derived classes do not get any special privileges. You cannot define a class in a friend declaration, but you can define a function, provided the class granting friendship is not local to a block. The function body is in the class scope, which affects name lookup (see Chapter 2). The friend function is automatically inline. Usually, friend functions are declared, not defined, in the class. A declaration or definition of a friend function does not make that function a member of the class and does not introduce the name into the class scope. Example 6-26 shows several different kinds of friends. Example 6-26. Friend functions and classes#include <iterator> // Simple container for singly-linked lists template<typename T> class slist { // Private type for a link (node) in the list template<typename U> struct link { link* next; U value; }; typedef link<T> link_type; // Base class for iterator and const_iterator. Keeps track of current node, and // previous node to support erase( ). class iter_base : public std::iterator<std::forward_iterator_tag, T> { protected: friend class slist; // So slist can construct iterators iter_base(slist::link_type* prv, slist::link_type* node); slist::link_type* node_; slist::link_type* prev_; }; public: typedef T value_type; typedef std::size_t size_type; class iterator : public iter_base { // Members omitted for bevity . . . private: friend class slist; // So slist can call constructor iterator(slist::link_type* prev, slist::link_type* node) : iter_base(prev, node) {} }; friend class iter_base; // So iter_base can use link_type friend class iterator; // So iterator can use link_type template<typename U> friend void swap(slist<U>& a, slist<U>& b); iterator begin( ) { return iterator(0, head_); } iterator end( ) { return iterator(0, 0); } private: link_type* head_; size_type count_; }; template<typename T> slist<T>::iter_base::iter_base(slist::link_type* prev, slist::link_type* node) : prev_(prev), node_(node) {} // Swap two lists in constant time by swapping members. template<typename T> void swap(slist<T>& a, slist<T>& b) { typename slist<T>::link_type* tmp_head = a.head_; typename slist<T>::size_type tmp_count = a.count_; a.head_ = b.head_; a.count_ = b.count_; b.head_ = tmp_head; b.count_ = tmp_count; } |