Ive got a problem that if I have a template class, which in turn has a template method that takes a parameter of another instance of the class (with different template arguments), that it can not access protected or private members of the class passed as a parameter, eg:
template<typename T>class MyClass
{
T v;
public:
MyClass(T v):v(v){}
template<typename T2>void foo(MyClass<T2> obj)
{
std::cout << v << " ";
//error C2248: 'MyClass<T>::v' : cannot access private member declared in class 'MyClass<T>'
std::cout << obj.v << " ";
std::cout << v + obj.v << std::endl;
}
};
int main()
{
MyClass<int> x(5);
MyClass<double> y(12.3);
x.foo(y);
}
Is there someway to say that methods in MyClass<T> have full access to MyClass<SomeOtherT>?