Declare a Friend function which is a template member function of adifferent class

Discussion in 'C++' started by getrakesh@gmail.com, Feb 26, 2008.

  1. Guest

    Hi All,

    I wanted to know how to declare a friend function which is a member
    function of a different class.

    Eg:
    /* Class which contains the member function to be declared as friend
    */
    template <class T, int W>
    class A
    {
    B obj;
    public:
    void foo(T val); // Function to be declared as a friend.
    }

    template <class T, int W>
    void A<T,W>::foo(val)
    {
    obj.val = val;
    }

    /* Class inside which the friend function is to be declared */
    template <class U>
    class B
    {
    U val;
    public:
    friend A<class T, class W>::foo(W val); // Declare friend.
    }

    I cant seem to get the syntax right. Please let me know.

    Thanks
    Rakesh
     
    , Feb 26, 2008
    #1
    1. Advertising

  2. Re: Declare a Friend function which is a template member functionof a different class

    schrieb:
    > /* Class inside which the friend function is to be declared */
    > template <class U>
    > class B
    > {
    > U val;
    > public:
    > friend A<class T, class W>::foo(W val); // Declare friend.
    > }
    >
    > I cant seem to get the syntax right. Please let me know.


    Something like that.

    template <class U>
    class B
    {
    U val;
    public:
    template <int W>
    friend void A<U, W>::foo(); // Declare friend.
    }

    But this won't work for your purpose anyway because of the cyclic
    dependancy of the definitions of class A and class B. A need to be
    defined befor B because the friend declaration won't work otherwise. B
    needs to be defined before A because it is a member of A.


    Marcel
     
    Marcel Müller, Feb 26, 2008
    #2
    1. Advertising

  3. Triple-DES Guest

    Re: Declare a Friend function which is a template member function ofa different class

    On 26 Feb, 10:34, wrote:
    > Hi All,
    >
    > I wanted to know how to declare a friend function which is a member
    > function of a different class.
    >
    > Eg:
    > /* Class which contains the member function to be declared as friend
    > */
    > template <class T, int W>
    > class A
    > {
    >    B obj;


    This causes a circular dependency. Also you are missing B's template
    parameter

    >    public:
    >    void foo(T val); // Function to be declared as a friend.
    >  }
    >
    > template <class T, int W>
    > void A<T,W>::foo(val)
    > {
    >   obj.val = val;
    >
    > }
    >
    > /* Class inside which the friend function is to be declared */
    > template <class U>
    > class B
    > {
    >   U val;
    >   public:
    >   friend A<class T, class W>::foo(W val); // Declare friend.


    Try:
    template<class T, int W>
    friend void A<T,W>::foo(W);
    >
    > }
    >
    > I cant seem to get the syntax right. Please let me know.


    Hope this helps.
    DP
     
    Triple-DES, Feb 26, 2008
    #3
    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. Replies:
    1
    Views:
    1,399
    Jim Langston
    Nov 9, 2006
  2. =?gb2312?B?wfXquw==?=
    Replies:
    10
    Views:
    710
    Victor Bazarov
    Aug 1, 2007
  3. slocum
    Replies:
    4
    Views:
    1,471
    Martin York
    Apr 10, 2008
  4. Rui Maciel
    Replies:
    11
    Views:
    1,119
    Francesco S. Carta
    Jul 13, 2010
  5. A L
    Replies:
    1
    Views:
    527
    Alf P. Steinbach /Usenet
    Aug 25, 2010
Loading...

Share This Page