nested class as friend

Alexandre Oliva oliva@dcc.unicamp.br
Fri Oct 31 06:18:00 GMT 1997


Luc Claeys writes:

> I want to make a nested class Y::Z a friend of class A.

Then the definition of class Y must have been provided already, and it
must contain at leat a declaration of nested class Z:

class Y {
  class Z;
// ...
};

class A {
  friend class Y::Z;
// ...
};

class Y::Z {
// ...
};

Your example 3:

> class Y;
> class Z;    // not true, but is a workaround for gcc 2.7.2

because gcc 2.7.2 is buggy.

> class A {
>    friend class Y::Z;
> };
> -----------^-------
>> g++ -c  -Wall   tst1.cc 
> tst1.cc:5: `Y' does not have a nested type named `Z'

maybe egcs should say that Y is not completely defined yet...

> This last version would be fine as long class Y is the only 
> class with a subclass Z (ARM Section 18.3.5 p407).

Nope, it would only be ok if Y were completely defined, and known to
have a nested type Z.

> I could even say 
>          friend class Z;

Only if Z were really a global class.

> However with subclasses like "iterator", many classes have
> a subclass with the name "iterator", we should not make them 
> all friends.

I assume wherever you refer to `subclass', you mean `nested class'.  A
subclass is a completely different animal :-)

In fact, you can't, as there can be at most a single class named
`iterator' in a namespace.

-- 
Alexandre Oliva
mailto:oliva@dcc.unicamp.br mailto:aoliva@acm.org
http://www.dcc.unicamp.br/~oliva
Universidade Estadual de Campinas, SP, Brasil



More information about the Gcc-bugs mailing list