這極小的例子將無法編譯,因爲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