how to declare friend for template class ?

Discussion in 'C++' started by slocum, Apr 9, 2008.

  1. slocum

    slocum Guest

    how to declare friend for template class ?

    class A
    {
    public:
    .....
    private:
    .....

    friend class B ????????
    }

    template <class T>
    class B
    {
    }
    slocum, Apr 9, 2008
    #1
    1. Advertising

  2. slocum

    Martin York Guest

    On Apr 9, 5:39 am, slocum <> wrote:
    > how to declare friend for template class ?
    >
    > class A
    > {
    > public:
    > .....
    > private:
    > .....
    >
    > friend class B ????????
    >
    > }
    >
    > template <class T>
    > class B
    > {
    >
    > }


    You can't.
    Though you can defined friends for particular versions of B.

    template<typename T>
    class B
    {
    };

    class A
    {
    friend class B<int>;
    friend class B<float>;
    friend class B<MyType>;
    // etc..
    };
    Martin York, Apr 9, 2008
    #2
    1. Advertising

  3. slocum

    slocum Guest

    And what about this way ??

    template <typename T>
    class B
    {

    };

    template <typename T>
    class A
    {
    friend class B<T>;

    };
    slocum, Apr 9, 2008
    #3
  4. slocum

    Brian Tyler Guest

    > You can't.
    > Though you can defined friends for particular versions of B.
    >
    > template<typename T>
    > class B
    > {
    > };
    >
    > class A
    > {
    > friend class B<int>;
    > friend class B<float>;
    > friend class B<MyType>;
    > // etc..
    > };


    That is completely untrue


    template<class T> class B
    {
    // Stuff
    };

    class A
    {
    template <class> friend class B;
    };

    makes B a friend irrespective of the type of it's template parameter.

    Brian.
    Brian Tyler, Apr 9, 2008
    #4
  5. slocum

    Martin York Guest

    On Apr 9, 6:03 am, Brian Tyler <> wrote:
    > > You can't.

    > That is completely untrue
    >
    > class A
    > {
    > template <class> friend class B;
    >
    > };
    >
    > makes B a friend irrespective of the type of it's template parameter.


    Live and learn.
    Martin York, Apr 10, 2008
    #5
    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. Oplec
    Replies:
    5
    Views:
    594
    Oplec
    Oct 28, 2003
  2. =?gb2312?B?wfXquw==?=
    Replies:
    10
    Views:
    685
    Victor Bazarov
    Aug 1, 2007
  3. Replies:
    2
    Views:
    654
    Triple-DES
    Feb 26, 2008
  4. Rui Maciel
    Replies:
    11
    Views:
    1,070
    Francesco S. Carta
    Jul 13, 2010
  5. A L
    Replies:
    1
    Views:
    503
    Alf P. Steinbach /Usenet
    Aug 25, 2010
Loading...

Share This Page