类模板中的友元声明
有三種友元聲明可以出現在類模板中:
1 非模板友元類或友元函數
函數 foo() 成員函數bar()以及 foobar類都是類模板QueueItem的所有實例的友元
class Foo { 
 ?void bar(); 
 }; 
 ?
 template <class T> 
 class QueueItem { 
 ?friend class foobar; 
 ?friend void foo(); 
 ?friend void Foo::bar(); 
 ?// ... 
 };
2 綁定的 bound 友元類模板或函數模板:
在類模板 QueueItem的實例和它的友元也是模板實例之間定義了一對一的映射,對 QueueItem的每一個類型的實例,foobar foo()和 Queue<T>::bar()的單個相關的實例都是友元
?class foobar{ ... };
?
template <class Type>
void foo( QueueItem<Type> );
?
template <class Type>
class Queue {
?void bar();
?
?// ...
};
?
template <class Type>
class QueueItem {
?friend class foobar<Type>;
?friend void foo<Type>( QueueItem<Type> );
?friend void Queue<Type>::bar();
?
?// ...
};
friend void foo<Type>( QueueItem<Type> );
函數名后面緊跟著顯式的模板實參表 foo<type> 這種語法可用來指定該友元聲明所引用的函數模板 foo()的實例。
如果省略了顯式的模板實參 如下所示:
friend void foo( QueueItem<Type> ); 
 則友元聲明會被解釋為引用了一個非模板函數,且該函數的參數類型是類模板 QueueItem的一個實例。
3 非綁定的 unbound 友元類模板或函數模板
 在類模板QueueItem的實例和其友元之間定義了一對多的映射,
對 QueueItem的每一個類型的實例 foobar foo()和 Queue<T>::bar()的所有實例都是友元,如下所示:
class QueueItem {
?template <class T>
? friend class foobar;
?
?template <class T>
? friend void foo( QueueItem<T> );
?
?template <class T>
? friend void Queue<T>::bar();
?
?// ...
}; 
我們應該注意 在標準 C++之前的編譯器不支持類模板中的這種友元聲明
轉載于:https://www.cnblogs.com/lidan/archive/2011/08/04/2239489.html
總結
 
                            
                        - 上一篇: hdu 2031 进制转换
- 下一篇: 「云端 JavaScript 漫游指南」
