template<typename T> class FooBar {};
template<typename T> class Bar {
friend class FooBar<T>;
};
template<typename T> class Bar2 {
template friend class FooBar<T>;
};
类 Bar 和 Bar2 有什么区别?
最佳答案
根据我的编译器,你的第二个语法无效。如果您将它们更改为:
template<typename T> class FooBar {};
template<typename T> class Bar {
friend class FooBar<T>;
};
template<typename T> class Bar2 {
template<typename T2> friend class FooBar;
};
然后它会编译。不同之处在于 Bar<T>
, 只有 FooBar<T>
是 friend ;如果你有 Bar<int>
, 只有 FooBar<int>
是 friend ,不是FooBar<char>
或任何其他类型,但 int
.在 Bar2<T>
, 任何类型的 FooBar
是 friend 。
关于c++ - 声明模板友元类的两种方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6970529/