【发布时间】:2015-11-25 19:59:56
【问题描述】:
在函数myfun 中有没有一种方法可以访问rhs.var 而无需编写返回var 的公共函数?另外,据我了解,这是因为 rhs 可能是不同的类型......这是正确的吗?
#include <iostream>
template<class T>
class foo
{
private:
T var;
public:
foo(T v) : var(v) {}
template<class Type>
void myfun(foo<Type>& rhs)
{
auto i = rhs.var; //BOOM
}
};
int main()
{
foo<int> a = 5;
foo<double> b = 2.2;
a.myfun(b);
}
【问题讨论】:
-
你可以在
foo中使用template <typename U> friend class foo; -
@Dieter Lücking 如此简单......从来没有想过