Board index » cppbuilder » Can template be a friend?

Can template be a friend?


2005-10-06 03:16:46 PM
cppbuilder91
Look at this code
template <class T>
class RvBase
{
friend class RvDerived; // how to declare a template friend here? or is
not possible at all?
}
template <class T>
// [C++ Error] test.h(152): E2356 Type mismatch in redeclaration of
'RvDerived'
class RvDerived: public RvBase<T>
{
}
Thanks in advance.
 
 

Re:Can template be a friend?

tinyabs wrote:
Quote
Look at this code

template <class T>
class RvBase
{
friend class RvDerived; // how to declare a template friend here? or is
not possible at all?
}

template <class T>
// [C++ Error] test.h(152): E2356 Type mismatch in redeclaration of
'RvDerived'
class RvDerived: public RvBase<T>
{
}

Thanks in advance.


Hello,
Ofcourse, a template class can be a friend, even if it is derived. But why would
you want to make a derived class a friend? Why don't you let it access protected
members of the base instead?
Here is the syntax of a template friend:
template <class T>
class RvBase
{
//* This is the correct way to make a template class friend.
//* Note the position of the friend keyword, it is not at the beginning.
template <class T>friend class RvDerived;
};
template <class T>
class RvDerived: public RvBase<T>
{
};
Cheers,
Wiljo.
 

Re:Can template be a friend?

Quote
Ofcourse, a template class can be a friend, even if it is derived. But why
would you want to make a derived class a friend? Why don't you let it
access protected members of the base instead?
Made a mistake, could have make it protected.
I know how now.
 

{smallsort}