template friend problem



 DEVELOP > c-Plus-Plus > template friend problem

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Mike - EMAIL IGNORED"
Date: 20 Nov 2003 06:59:32 PM
Object: template friend problem
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.
.

User: "Victor Bazarov"

Title: Re: template friend problem 20 Nov 2003 10:15:54 PM
"Mike - EMAIL IGNORED" <m_d_berger_1900@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
.
User: "Mike - EMAIL IGNORED"

Title: Re: template friend problem 21 Nov 2003 06:15:11 AM
Victor Bazarov wrote:


"Mike - EMAIL IGNORED" <m_d_berger_1900@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.
.
User: "tom_usenet"

Title: Re: template friend problem 21 Nov 2003 08:20:39 AM
On Fri, 21 Nov 2003 07:15:11 -0500, Mike - EMAIL IGNORED
<m_d_berger_1900@yahoo.com> wrote:



Victor Bazarov wrote:


"Mike - EMAIL IGNORED" <m_d_berger_1900@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
.




  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER