[英]Class templates and friend Classes
我有Node類,而BinaryTree類是其中一個Node類型元素的朋友。 我想制作任何類型的BinareTree,所以我在兩個類上都使用了模板。 像這樣的代碼:
template <class T>
class Node
{
T value;
Node<T> *left, *right;
friend template <typename T> class BinaryTree; // here is the problem
};
template <class Y>
class BinaryTree{...};
如果要使用它作為模板,在朋友類BinaryTree的聲明中我需要什么語法? 我的目標是能夠寫:
BinareTree<int> tree;
我想到了更好的方法嗎? 謝謝 !
如果您查找模板friends的語法,則會找到正確的方法:
class A {
template<typename T>
friend class B; // every B<T> is a friend of A
template<typename T>
friend void f(T) {} // every f<T> is a friend of A
};
盡管您可能只想與特定的朋友:
friend class BinaryTree<T>;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.