这极小的例子将无法编译,因为A<int>
不能A<double>
如何让方法访问其他模板类实例的私有成员?
template <class T>
class A {
int i;
public:
template <class U>
void copy_i_from(const A<U> & a){
i = a.i;
}
};
int main(void) {
A<int> ai;
A<double> ad;
ai.copy_i_from(ad);
return 0;
}
访问私有成员i
我知道,我可以让对方(见:How to access private members of other template class instances?)的所有模板实例朋友,但因为我只有一种方法需要访问权限(如示例中所示),我宁愿将友谊限制为该方法。这可能吗?
致敬:我想知道我能做些什么来改善问题! – DarioP 2014-10-17 10:26:22
这两个答案和问题本身都默默地降低了;我怀疑它与内容无关。 – Angew 2014-10-17 10:35:50