Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Nested friend class in nested template problem

Reply
Thread Tools

Nested friend class in nested template problem

 
 
tonvandenheuvel@gmail.com
Guest
Posts: n/a
 
      12-07-2007
Hi all,

the following code does not compile in gcc 4.0.2:


3 class Outer
4 {
5 class B;
6
7 template<typename T>
8 class A
9 {
10 friend class Outer::B;
11
12 public:
13 A(T t): a(t) { };
14 A() { };
15 protected:
16 T a;
17 };
18
19 class B : public A<int>
20 {
21 public:
22 B(A<int> & a)
23 {
24 b = a.a;
25 }
26
27 int b;
28 };
29 };

The error I get is:

friend.cpp: In constructor 'Outer::B::B(Outer::A<int>&)':
friend.cpp:16: error: 'int Outer::A<int>::a' is protected
friend.cpp:24: error: within this context

Note that when the Outer class is stripped, everything compiles just
fine. What is the problem here?
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      12-07-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Hi all,
>
> the following code does not compile in gcc 4.0.2:
>
>
> 3 class Outer
> 4 {
> 5 class B;
> 6
> 7 template<typename T>
> 8 class A
> 9 {
> 10 friend class Outer::B;
> 11
> 12 public:
> 13 A(T t): a(t) { };
> 14 A() { };
> 15 protected:
> 16 T a;
> 17 };
> 18
> 19 class B : public A<int>
> 20 {
> 21 public:
> 22 B(A<int> & a)
> 23 {
> 24 b = a.a;
> 25 }
> 26
> 27 int b;
> 28 };
> 29 };


Please don't post line numbers like you have here. It makes it
quite difficult to quickly copy-and-paste your code and compile it.
You can always use *comments* to identify the lines to which the
compiler is referring in its error messages.

Thanks.

>
> The error I get is:
>
> friend.cpp: In constructor 'Outer::B::B(Outer::A<int>&)':
> friend.cpp:16: error: 'int Outer::A<int>::a' is protected
> friend.cpp:24: error: within this context
>
> Note that when the Outer class is stripped, everything compiles just
> fine. What is the problem here?


A buggy compiler?

Your code compiles OK in Comeau online (4.3.9) or VC++ 2005 SP1.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
 
 
 
want.to.be.professer
Guest
Posts: n/a
 
      12-07-2007
On Dec 7, 10:40 pm, (E-Mail Removed) wrote:
> Hi all,
>
> the following code does not compile in gcc 4.0.2:
>
> 3 class Outer
> 4 {
> 5 class B;
> 6
> 7 template<typename T>
> 8 class A
> 9 {
> 10 friend class Outer::B;
> 11
> 12 public:
> 13 A(T t): a(t) { };
> 14 A() { };
> 15 protected:
> 16 T a;
> 17 };
> 18
> 19 class B : public A<int>
> 20 {
> 21 public:
> 22 B(A<int> & a)
> 23 {
> 24 b = a.a;
> 25 }
> 26
> 27 int b;
> 28 };
> 29 };
>
> The error I get is:
>
> friend.cpp: In constructor 'Outer::B::B(Outer::A<int>&)':
> friend.cpp:16: error: 'int Outer::A<int>::a' is protected
> friend.cpp:24: error: within this context
>
> Note that when the Outer class is stripped, everything compiles just
> fine. What is the problem here?


Note the error message:'int Outer::A<int>::a' is protected.

B is inherited form A as public inheritance,
So B can only call A's public and private members.
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      12-07-2007
want.to.be.professer wrote:
> On Dec 7, 10:40 pm, (E-Mail Removed) wrote:
>> Hi all,
>>
>> the following code does not compile in gcc 4.0.2:
>>
>> 3 class Outer
>> 4 {
>> 5 class B;
>> 6
>> 7 template<typename T>
>> 8 class A
>> 9 {
>> 10 friend class Outer::B;
>> 11
>> 12 public:
>> 13 A(T t): a(t) { };
>> 14 A() { };
>> 15 protected:
>> 16 T a;
>> 17 };
>> 18
>> 19 class B : public A<int>
>> 20 {
>> 21 public:
>> 22 B(A<int> & a)
>> 23 {
>> 24 b = a.a;
>> 25 }
>> 26
>> 27 int b;
>> 28 };
>> 29 };
>>
>> The error I get is:
>>
>> friend.cpp: In constructor 'Outer::B::B(Outer::A<int>&)':
>> friend.cpp:16: error: 'int Outer::A<int>::a' is protected
>> friend.cpp:24: error: within this context
>>
>> Note that when the Outer class is stripped, everything compiles just
>> fine. What is the problem here?

>
> Note the error message:'int Outer::A<int>::a' is protected.
>
> B is inherited form A as public inheritance,
> So B can only call A's public and private members.


Actually your post made me think a bit more about this, and perhaps
I was too fast to judge GCC as buggy...

Usually the rule is, the derived class is not alloed to access
*another instance's protected members*, but only its own or of
another instace of _its own type_. IOW, you should get an error
in 'foo', but not in 'bar':

class A {
protected:
int a;
};

class B : A {
void foo(A& a) {
a.a; /// error!
}

void bar() {
this->a; // OK
}
};

However, in this particular case, since A<T> makes 'B' its
friend, all the rules about protected access are overridden by
the rules of friendship, which allows access to _all_ members
regardless of their declared access specifiers.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Declaring a template class with two template params a friend in anon-template class A L C++ 1 08-25-2010 07:25 AM
problem with a function template returning a private nested class ofa class template ymost@hotmail.com C++ 2 12-28-2008 10:43 AM
Is nested class automatically friend of class that it is nested in? request@no_spam.com C++ 5 09-25-2006 08:31 AM
Problem with defining template friend function of a template class. PengYu.UT@gmail.com C++ 2 11-09-2005 08:27 PM
A parameterized class (i.e. template class / class template) is not a class? christopher diggins C++ 16 05-04-2005 12:26 AM



Advertisments