Re: Templates and friends and template methods with private methods.

Discussion in 'C++' started by Buster Copley, Jul 5, 2003.

  1. Gianni Mariani wrote:
    > I have a 2 co-operating 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
    Buster Copley, Jul 5, 2003
    #1
    1. Advertising

  2. I didn't see John's reply, despite your warning.
    I'm getting used to some new news software.

    Good luck,
    Buster
    Buster Copley, Jul 5, 2003
    #2
    1. Advertising

  3. >
    > Buster, thanks for your reply. However the cigar is still in the box.
    >
    > Either gcc 3.3 does not accept this or I am doing somthing silly.
    >
    > This shows up as syntax errors
    > template <typename A> class X {
    > friend template <typename Z> class Y;
    > };
    >
    >
    > This forward declates Y in the scope of class X i.e. X::Y
    > template <typename A> class X {
    > template <typename Z> class Y;
    > };
    >
    > This does not end up making a friend of anything
    > template <typename A> class X {
    > template <typename Z> class ::Y;
    > };
    >
    > Note that this is all as per gcc 3.3, it could be a problem with gcc.
    >
    > The last one looked promising but I get errors regarding accessing
    > private members.
    >
    > The syntax:
    > friend template <typename Z> class ::Y;
    >
    > gives numerous errors.
    >


    Did you not see my earlier post?

    template <typename A>
    class Y;

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

    john
    John Harrison, Jul 6, 2003
    #3
  4. John Harrison wrote:

    >
    > Did you not see my earlier post?


    Nope - my news server is truly messed up at the moment.

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


    <rant>
    Oooh. (Fat lady sings.... Lighting cigar ... )

    Darn, I shoulda guessed that one. A friend *template* ... that makes
    sense. The deduction is that the C++ semantics of a friend models that
    of any member i.e. a "friend" is like an attribute of a member to a class.
    </rant>

    The code below compiles without a peep with gcc 3.3. MSVC++(SP5)
    chunders at least 16 errors. I need to get a copy of a better native MS
    win32 compiler.


    template <typename A> class X;
    template <typename A> class Y;

    template <typename A>
    class X
    {

    template <typename Z> friend class Y;
    private:

    A m_a;

    public:
    X( A i_val );

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

    template <typename A>
    class Y
    {

    template <typename Z> friend class X;
    private:

    A m_a;

    public:
    Y( A i_val );

    template<typename B> void DoOtherThing( X<B> & b )
    {
    m_a = b.m_a;
    }
    };

    int main()
    {

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

    xs.DoThing( ys );
    xs.DoThing( yi );

    yi.DoOtherThing( xs );
    }
    Gianni Mariani, Jul 6, 2003
    #4
  5. Buster Copley wrote:
    > I'm glad you've got it sorted out. I did mean to include the word friend
    > in there; that's what I had in emacs, but I missed it out when I was
    > retyping the message.
    >
    > The template friend declaration is a forward declaration for the
    > template, so you can delete the other forward declarations if you put
    > the two classes one way round.
    >


    Thanks to both John and you for taking the time to answer the question.
    Gianni Mariani, Jul 7, 2003
    #5
  6. John Harrison wrote:

    >
    > Did you not see my earlier post?
    >


    FYI ... I think the problem might be on your end, it's not in Google
    either ...

    Thx
    Gianni Mariani, Jul 7, 2003
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JKop
    Replies:
    3
    Views:
    467
  2. BigMan
    Replies:
    4
    Views:
    3,613
    Malte Starostik
    Mar 29, 2005
  3. Replies:
    2
    Views:
    596
  4. recover
    Replies:
    2
    Views:
    799
    recover
    Jul 25, 2006
  5. Replies:
    0
    Views:
    651
Loading...

Share This Page