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

G

getrakesh

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
 
M

Marcel Müller

/* 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
 
T

Triple-DES

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 said:
}

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

Hope this helps.
DP
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,153
Members
46,699
Latest member
AnneRosen

Latest Threads

Top