templates and friends

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gianni Mariani

    templates and friends


    I have 2 distinct template classes which co-operate, hence are friends.
    However, I can't seem to figure out what syntax to use to make this work.

    What is the right(tm) way to write a friend class here ?

    Here is the code:

    template <typename A>
    class Y;

    template <typename A>
    class X
    {
    friend class Y<A>;
    private:

    A m_a;

    public:

    X( A i_val );

    template<typena me B> void DoThing( Y<B> & b )
    {
    m_a = b.m_a;
    }

    };

    template <typename A>
    class Y
    {
    friend class X<A>;
    private:

    A m_a;

    public:

    Y( A i_val );

    template<typena me B> void DoOtherThing( Y<B> & b )
    {
    m_a = b.m_a;
    }

    };



    int main()
    {

    Y<int> yi( 1 );
    X<short> xs( 2 );

    xs.DoThing( yi );

    }

    Here is the error:

    g++ -c -o testfriend.o testfriend.cpp
    testfriend.cpp: In member function `void X<A>::DoThing(Y <B>&) [with B =
    int, A
    = short int]':
    testfriend.cpp: 52: instantiated from here
    testfriend.cpp: 31: error: `int Y<int>::m_a' is private
    testfriend.cpp: 20: error: within this context
    make: *** [testfriend.o] Error 1



  • John Harrison

    #2
    Re: templates and friends


    "Gianni Mariani" <gi2nospam@mari ani.ws> wrote in message
    news:be5a2a$2ct @dispatch.conce ntric.net...[color=blue]
    >
    > I have 2 distinct template classes which co-operate, hence are friends.
    > However, I can't seem to figure out what syntax to use to make this[/color]
    work.[color=blue]
    >
    > What is the right(tm) way to write a friend class here ?
    >
    > Here is the code:
    >
    > template <typename A>
    > class Y;
    >
    > template <typename A>
    > class X
    > {
    > friend class Y<A>;
    > private:
    >
    > A m_a;
    >
    > public:
    >
    > X( A i_val );
    >
    > template<typena me B> void DoThing( Y<B> & b )
    > {
    > m_a = b.m_a;
    > }
    >
    > };
    >
    > template <typename A>
    > class Y
    > {
    > friend class X<A>;
    > private:
    >
    > A m_a;
    >
    > public:
    >
    > Y( A i_val );
    >
    > template<typena me B> void DoOtherThing( Y<B> & b )
    > {
    > m_a = b.m_a;
    > }
    >
    > };
    >
    >
    >
    > int main()
    > {
    >
    > Y<int> yi( 1 );
    > X<short> xs( 2 );
    >
    > xs.DoThing( yi );
    >
    > }
    >
    > Here is the error:
    >
    > g++ -c -o testfriend.o testfriend.cpp
    > testfriend.cpp: In member function `void X<A>::DoThing(Y <B>&) [with B =
    > int, A
    > = short int]':
    > testfriend.cpp: 52: instantiated from here
    > testfriend.cpp: 31: error: `int Y<int>::m_a' is private
    > testfriend.cpp: 20: error: within this context
    > make: *** [testfriend.o] Error 1
    >[/color]

    You have to distinguish between

    1) All template instances are friends of each other, e.g. X<int> is a friend
    of Y<double>

    2) Only templates instantiated with the same type are friend of each other,
    e.g. X<int> is a friend of Y<int> but not of Y<double>.

    You want case 1, but the code you've written is for case 2. Try this

    template <typename A>
    class Y;

    template <typename A>
    class X
    {
    template <typename B>
    friend class Y;

    etc.

    john


    Comment

    Working...