久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

類模板與模板類朋友,這里到底發生了什么?

Class template with template class friend, what#39;s really going on here?(類模板與模板類朋友,這里到底發生了什么?)
本文介紹了類模板與模板類朋友,這里到底發生了什么?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

假設我正在為二叉樹創建一個類 BT,并且我有一個描述樹元素的類 BE,類似于

Let's say I'm creating a class for a binary tree, BT, and I have a class which describes an element of the tree, BE, something like

template<class T> class BE {
    T *data;
    BE *l, *r;
public:
...
    template<class U> friend class BT;
};

template<class T> class BT {
    BE<T> *root;
public:
...
private:
...
};

這似乎有效;但是我對下面發生的事情有疑問.

This appears to work; however I have questions about what's going on underneath.

我最初嘗試將朋友聲明為

I originally tried to declare the friend as

template<class T> friend class BT;

然而,這里似乎有必要使用 U(或除 T 以外的其他東西),這是為什么呢?這是否意味著任何特定的 BT 是任何特定 BE 類的朋友?

however it appears necessary to use U (or something other than T) here, why is this? Does it imply that any particular BT is friend to any particular BE class?

關于模板和朋友的 IBM 頁面提供了不同類型的函數朋友關系的示例,而不是類(并且猜測語法尚未收斂到解決方案上).我更愿意了解如何為我希望定義的朋友關系類型獲得正確的規范.

The IBM page on templates and friends has examples of different type of friend relationships for functions but not classes (and guessing a syntax hasn't converged on the solution yet). I would prefer to understand how to get the specifications correct for the type of friend relationship I wish to define.

推薦答案

template<class T> class BE{
  template<class T> friend class BT;
};

不允許,因為模板參數不能相互遮蔽.嵌套模板必須具有不同的模板參數名稱.

Is not allowed because template parameters cannot shadow each other. Nested templates must have different template parameter names.

template<typename T>
struct foo {
  template<typename U>
  friend class bar;
};

這意味著 barfoo 的朋友,而不管 bar 的模板參數如何.barbarbar 和任何其他 bar 都是朋友foo.

This means that bar is a friend of foo regardless of bar's template arguments. bar<char>, bar<int>, bar<float>, and any other bar would be friends of foo<char>.

template<typename T>
struct foo {
  friend class bar<T>;
};

這意味著當 bar 的模板參數匹配 foo 的模板參數時,barfoo 的朋友.只有 barfoo 的朋友.

This means that bar is a friend of foo when bar's template argument matches foo's. Only bar<char> would be a friend of foo<char>.

在您的情況下,friend class bar; 應該就足夠了.

In your case, friend class bar<T>; should be sufficient.

這篇關于類模板與模板類朋友,這里到底發生了什么?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How can I read and manipulate CSV file data in C++?(如何在 C++ 中讀取和操作 CSV 文件數據?)
In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫 for() 循環: for( int i = 1, double i2 = 0;)
How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環?)
Reusing thread in loop c++(在循環 C++ 中重用線程)
Precise thread sleep needed. Max 1ms error(需要精確的線程睡眠.最大 1ms 誤差)
Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環形?)
主站蜘蛛池模板: 精品成人在线 | 日韩一二区 | 我要看一级片 | 日韩av电影在线观看 | 国产一区二区三区四区五区加勒比 | 综合精品久久久 | 3p视频在线观看 | 亚洲精品精品 | 成人影 | 久久久视频在线 | 久久国产秒 | 男女下面一进一出网站 | 欧美一区二区三区在线视频 | 亚洲精品免费在线观看 | 日本超碰 | 欧美日韩在线高清 | 国产精品自拍视频 | 久久久久久久夜 | 亚洲精品一区中文字幕乱码 | 精品免费在线 | 午夜资源 | 精品视频在线免费观看 | www.一区二区 | 一级毛片大全免费播放 | 91久久视频 | 午夜在线免费观看视频 | 亚洲国产中文字幕 | 久久久精品一区 | 日韩中文在线 | 亚洲免费观看 | 亚洲视频免费在线 | 国产在线二区 | 国产精品精品视频一区二区三区 | 性视频一区 | 欧美日韩高清一区 | 欧美色综合一区二区三区 | 久久精品aaa | av影音资源 | 国产清纯白嫩初高生在线播放视频 | 国产超碰人人爽人人做人人爱 | 欧美中文字幕一区二区三区亚洲 |