I have a two template class, templateClass1 and templateClass2. I want to use private variables and methods of templateClass1 in templateClass2. Is it possible to do so by using friend keyword in c++ ?
Sumit
I have a two template class, templateClass1 and templateClass2. I want to use private variables and methods of templateClass1 in templateClass2. Is it possible to do so by using friend keyword in c++ ?
Sumit
I know this post is probably dead, but for other people who stumble upon this...
templateClass1.h
template <class T> class templateClass2; // forward declare
template <typename T>
class templateClass1 {
friend templateClass2<T>;
};
templateClass2.h
template <class T> class templateClass1; // forward declare
template <typename T>
class templateClass2 {
friend templateClass1;
}
It is possible to have a friends of any type, but a template is not a type until its template arguments have been supplied. So in general you would have to have a specialization for each full type you wish to be friends with. This will push you toward attempting to pass the type to be friend as a template parameter, but you can not supply a template type that will be friended.
for ex. this is illegal
template <class T>
class A
{
friend class T;
};
with those stipulations it makes it very difficult to do anything meaning full with templates and friendedness.