By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,388 Members | 1,823 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,388 IT Pros & Developers. It's quick & easy.

template friend problem

P: n/a
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.
Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"Mike - EMAIL IGNORED" <m_*************@yahoo.com> wrote...
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
Jul 22 '05 #2

P: n/a


Victor Bazarov wrote:

"Mike - EMAIL IGNORED" <m_*************@yahoo.com> wrote...
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

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

Sorry,
Mike.
Jul 22 '05 #3

P: n/a
On Fri, 21 Nov 2003 07:15:11 -0500, Mike - EMAIL IGNORED
<m_*************@yahoo.com> wrote:


Victor Bazarov wrote:

"Mike - EMAIL IGNORED" <m_*************@yahoo.com> wrote...
> 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

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
Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.