Topic: forward class declarations
Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 1995/04/13 Raw View
esap@kirjorastas.cs.tut.fi (Pulkkinen Esa) writes:
>How about this one:
>class c1;
>class c2
>{
> class c1;
> friend class c1;
> c1 *member;
>};
>class c2::c1 { /* ... */ };
>Which one of c1's will be a friend of c2? the file scoped or the one
>declared in c2?
class c2::c1 is a friend of c2. The file-scope c1 is not a friend.
>Does the interpretation change, if you switch the
>order of the two declarations inside the class c2?
If you switch the first two, yes. Then the file-scope c1 is a friend
of c2, but c2::c1 is not.
It's actually not hard to understand if you apply ordinary scoping rules.
CASE 1:
=======
class c1;
class c2 {
friend class c1; // at this point, the only visible c1 is at file scope
class c1; // now we declare c2::c1, which is not a friend
};
CASE 2:
=======
class c1;
class c2 {
class c1; // declare c2::c1
friend c1; // we have a c1 declared in the current scope
// so that is the one we mean: c2::c1
// the only way to refer to the file-scope c1 here is to write "::c1"
};
--
Steve Clamage, stephen.clamage@eng.sun.com
Author: chris@alofi.etca.fr (Christian Millour)
Date: 1995/04/14 Raw View
In article <3mia7k$7p2@engnews2.Eng.Sun.COM>, clamage@Eng.Sun.COM (Steve Clamage) writes:
|>
|> CASE 1:
|> =======
|> class c1;
|> class c2 {
|> friend class c1; // at this point, the only visible c1 is at file scope
|> class c1; // now we declare c2::c1, which is not a friend
|> };
|>
Actually, you shouldn't need a visible c1 since a friend
declaration is a true declaration, which should act as a
forward declaration in the same scope as the class making
the friend declaration if the friend class hasn't been
declared yet (2nd, r9.1). So you should be able to write
class c2 {
friend class c1; // declares a friend class in the same scope as c2
// (isa forward declaration if c1 ain't declared yet)
class c1; // declares c2::c1, not a friend.
};
In practice some compilers have trouble with this rule when
it comes to nested classes.
class c3 {
// class c1; // should not be necessary
class c2 {
friend class c1; // should forward declare c3::c1
int i;
class c1 {
public:
c1(c2& x);
};
};
class c1 {
public:
c1(c2& x);
};
};
class c1 {
public:
c1(c3::c2& x);
};
c3::c2::c1::c1(c3::c2& x) {x.i++;} // error (correct)
c3::c1::c1(c3::c2& x) {x.i++;} // error for cfront 3.0.2 (bogus)
c1::c1(c3::c2& x) {x.i++;} // OK for front 3.0.2 (bogus)
for the code above we get with cfront 3.0.2
"__.cc", line 24: error: c3::c2::c1::c1() cannot access c3::c2::i: private member
"__.cc", line 25: error: c3::c1::c1() cannot access c3::c2::i: private member
uncommenting line 2 gives
"__.cc", line 24: error: c3::c2::c1::c1() cannot access c3::c2::i: private member
"__.cc", line 26: error: c1::c1() cannot access c3::c2::i: private member
According to 2nd r9.1 there should be no difference. Am I missing
something ? What is the standard rule ?
--chris@etca.fr
|> --
|> Steve Clamage, stephen.clamage@eng.sun.com
Author: schuenem@Informatik.TU-Muenchen.DE (Ulf Schuenemann)
Date: 1995/04/12 Raw View
In article <3mag9a$ktk@engnews2.Eng.Sun.COM>, clamage@Eng.Sun.COM (Steve Clamage) writes:
[..]
|> >//class c1;
|>
|> >class c2
|> > {
|> > friend class c1;
|>
|> > c1* member;
|> > };
[..]
|> Because of possible confusion, it is probably better alwasy to have a
|> file-scope forward declaration for any file-scope class which
|> is referenced before it is defined.
Can't I express the same with a qualified name?
class c2 {
friend class ::c1;
c1* member;
};
This would also be usefull with memberclass-friends:
class Outer {
// class Outer::Inner; -- don't need this line any more
friend class ::Outer::Inner;
class Inner {
// use private parts of Outer
};
};
But it seems that friends can only be relativ (qualified) names
but not absolut qualified names (qualified name starting with ::).
Is there a reason for this restricition?
Ulf Schuenemann
--------------------------------------------------------------------
Ulf Sch nemann
Fakult t f r Informatik, Technische Universit t M nchen, Germany.
email: schuenem@informatik.tu-muenchen.de
Author: esap@kirjorastas.cs.tut.fi (Pulkkinen Esa)
Date: 1995/04/11 Raw View
Steve Clamage (clamage@Eng.Sun.COM) wrote:
: jzipnick@best.com (Jay Zipnick) writes:
: >Is the following code legal?
: >-------------
: >//class c1;
: >class c2
: > {
: > friend class c1;
: > c1* member;
: > };
: >-------------
: This area of C++ has been ill-defined in the past, but the draft
: standard has clarified it. It is not surprising that you get
: different results with different compilers.
: Under the clarified rules, your example has the same meaning with
: and without the line you commented out. Either way, a file-scope
: class c1 is a friend of class c2.
: Because of possible confusion, it is probably better alwasy to have a
: file-scope forward declaration for any file-scope class which
: is referenced before it is defined.
How about this one:
class c1;
class c2
{
class c1;
friend class c1;
c1 *member;
};
class c2::c1 { /* ... */ };
Which one of c1's will be a friend of c2? the file scoped or the one
declared in c2? Does the interpretation change, if you switch the
order of the two declarations inside the class c2?
--
Esa Pulkkinen | C++ programmers do it virtually
E-Mail: esap@cs.tut.fi | everywhere with a class, resulting
WWW : http://www.cs.tut.fi/~esap/ | in multiple inheritance.
Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 1995/04/10 Raw View
jzipnick@best.com (Jay Zipnick) writes:
>Is the following code legal?
>-------------
>//class c1;
>class c2
> {
> friend class c1;
> c1* member;
> };
>-------------
>The forward class declaration "class c1;" is intentionally commented out.
>The declaration "friend class c1;" seems to act as a forward class
>declaration in 6 out of 7 compilers I have tested.
This area of C++ has been ill-defined in the past, but the draft
standard has clarified it. It is not surprising that you get
different results with different compilers.
Under the clarified rules, your example has the same meaning with
and without the line you commented out. Either way, a file-scope
class c1 is a friend of class c2.
Because of possible confusion, it is probably better alwasy to have a
file-scope forward declaration for any file-scope class which
is referenced before it is defined.
--
Steve Clamage, stephen.clamage@eng.sun.com
Author: jzipnick@best.com (Jay Zipnick)
Date: 1995/04/07 Raw View
Is the following code legal?
-------------
//class c1;
class c2
{
friend class c1;
c1* member;
};
-------------
The forward class declaration "class c1;" is intentionally commented out.
The declaration "friend class c1;" seems to act as a forward class
declaration in 6 out of 7 compilers I have tested.
However, looking in the in the ARM and elsewhere, I did not see anything
that explicitly stated that a friend class declaration acts as a forward
class declaration in the same scope. I would expect it would, and six
compilers agree with me, but that seventh compiler says otherwise.
Author: rac@intrigue.com (Robert Coie)
Date: 1995/04/08 Raw View
In article <jzipnick-0704951700430001@jzipnick.vip.best.com>,
jzipnick@best.com (Jay Zipnick) wrote:
: Is the following code legal?
:
: -------------
: //class c1;
:
: class c2
: {
: friend class c1;
:
: c1* member;
: };
: -------------
:
: The forward class declaration "class c1;" is intentionally commented out.
: The declaration "friend class c1;" seems to act as a forward class
: declaration in 6 out of 7 compilers I have tested.
:
: However, looking in the in the ARM and elsewhere, I did not see anything
: that explicitly stated that a friend class declaration acts as a forward
: class declaration in the same scope. I would expect it would, and six
: compilers agree with me, but that seventh compiler says otherwise.
The majority is always right.
The C++ Programming Language (2nd edition) p.162:
"A friend declaration is a real declaration. It introduces the name of
the function into the scope enclosing the class in which it was declared a
friend and is checked against other declarations of that name in that
scope."
Although the specific wording speaks of friend function declarations, I
can see no reason why friend class declarations should behave any
differently.
Robert Coie rac@intrigue.com
Implementor, Intrigue Corporation AppleLink: INTRIGUE
Author: arthur@solomon.technet.sg (Arthur Sombrito)
Date: Thu, 5 Nov 1992 09:11:04 GMT Raw View
Hi,
I'd like to ask the ARM specifications regarding forward class
declarations. At what scope would the class name be entered?
The current scope or the first non-class enclosing scope?
For example:
class A {
class B {
struct C; // is it ::C or A::B::C ?
struct D *dP; // likewise is it ::D or A::B::D ?
void f(struct E *eP); // is it ::E or is it entered in
// the local scope of f() ?
};
};
C *cP; // so is C visible?
E *eP; // and so is E?
The USL library assumes that forward class declarations are
entered in the first non-class enclosing scope. As far as I
could tell in ARM, it should be entered in the current scope.
Which should it be?
Thanks for any replies