日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

类模板中的友元声明

發布時間:2025/4/14 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 类模板中的友元声明 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

有三種友元聲明可以出現在類模板中:

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()的單個相關的實例都是友元

template <class Type>
?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()的所有實例都是友元,如下所示:

template <class Type>
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

總結

以上是生活随笔為你收集整理的类模板中的友元声明的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。