Gianni Mariani wrote:
> I have a 2 cooperating class templates that need to peer into each
> other's privates. Each of these have template methods. How does one
> manage friendness in this scenario ? See code below:
>
> template <typename A>
> class Y;
>
> template <typename A>
> class X
> {
>
> friend class Y<A>;
// The above makes Y <T> a friend of X <T>, for each
// specialization (instantiation) of X <T>. So in
// your main (), the only friendship granted is that
// Y <int> is a friend of X <int>.
// If you make the template class Y a friend of X, as:
template <typename T> class Y;
// it has the effect of making every specialization
// of Y a friend of every specialization of X.
// Plus, it counts as a forward declaration of Y,
// so you get to have two fewer lines of code.
> private:
>
> A m_a;
>
> public:
>
> X( A i_val );
>
> template<typename B> void DoThing( Y<B> & b )
> {
> m_a = b.m_a;
> }
>
> };
I hope this helps. I learned about it in the excellent
chapter on templates in Ray Lischner's "C++ In A Nutshell".
Buster Copley
