日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

类模板中的友元声明

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

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

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

總結

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

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