template friend problem

  • Thread starter Mike - EMAIL IGNORED
  • Start date
M

Mike - EMAIL IGNORED

Following 14.5.3, I tried:

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

where A is a simple non-template class and B is a template.
I tried to use a private method in B from a method in A.

Using gcc-3.2, this fails at compile time, but succeeds
when the method in B is made public.

Is this a compiler bug, or am I missing something?

Thanks for your help.
Mike.
 
V

Victor Bazarov

Mike - EMAIL IGNORED said:
Following 14.5.3, I tried:

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

where A is a simple non-template class and B is a template.
I tried to use a private method in B from a method in A.

I think you are mistaken about the friendship _direction_.
In the situation you showed, 'B' is a friend of 'A', which
means that any member function of 'B' has access to private
members of 'A', not the other way around.
Using gcc-3.2, this fails at compile time, but succeeds
when the method in B is made public.

Is this a compiler bug, or am I missing something?

You're missing something.

Victor
 
M

Mike - EMAIL IGNORED

Victor said:
I think you are mistaken about the friendship _direction_.
In the situation you showed, 'B' is a friend of 'A', which
means that any member function of 'B' has access to private
members of 'A', not the other way around.


You're missing something.

Victor
My mistake, I tried to use a method in A from the template B.
Am I still missing something?

Sorry,
Mike.
 
T

tom_usenet

My mistake, I tried to use a method in A from the template B.
Am I still missing something?

Yes, this compiles fine in GCC 3.2:

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

int i; //private
};

template <class T>
class B
{
public:
int f()
{
A a;
a.i = 5; //private access
return a.i;
}
};

int main()
{
B<int> b;
b.f();
}

Tom
 

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
474,147
Messages
2,570,833
Members
47,380
Latest member
AlinaBlevi

Latest Threads

Top