Topic: forward declarations of nested classes


Author: "Pavel Kuznetsov" <pavel_kuznetsov@no.spam.users.sourceforge.net>
Date: Thu, 7 Jun 2001 16:45:37 GMT
Raw View
> > There is a very large advantage to being able to forward declare a
> > nested class, since you can often avoid having to import the entire
> > declaration of the outer class.  This can greatly reduce the coupling
> > between modules and shorten compile time considerably.

> If you want to forward-declare a class, do not make it nested:
>
> class A_local
> {
> };
>
> class A
> {
> public:
>   typedef A_local Local;
> };

The intention was to avoid using faked nesting
when real nesting is provided by the language.
e.g.

class A
{
public:
  class B_C
  {
  };
};

class A_B
{
public:
  class C
  {
  };
};

Locality is good...

--
Pavel Kuznetsov
mailto:pavel_kuznetsov@no.spam.users.sourceforge.net
remove "no.spam." from e-mail address


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: James Dennett <jdennett@acm.org>
Date: Tue, 5 Jun 2001 14:41:30 GMT
Raw View
Pavel Kuznetsov wrote:
>
> It would be nice if it was allowed to write something like:
>
> class A;
> class A::Local;
> A::Local* p;
>
> Is there any problem allowing that?

Suppose that class A is defined in another translation unit,
as

class A
{
private:  struct Local { };
};

Then your access to A::Local should have been disallowed by
access control, but the compiler has no way to know this.

That's apart from problems where A::Local is not a class;
I suspect that most of those would be picked up during
linking.  The access control problem would not be detected
at all by most current compilers, as far as I know.

-- James Dennett

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: Jonathan Biggar <jon@floorboard.com>
Date: Tue, 5 Jun 2001 18:41:51 GMT
Raw View
James Dennett wrote:
> > It would be nice if it was allowed to write something like:
> >
> > class A;
> > class A::Local;
> > A::Local* p;
>
> Suppose that class A is defined in another translation unit,
> as
>
> class A
> {
> private:  struct Local { };
> };
>
> Then your access to A::Local should have been disallowed by
> access control, but the compiler has no way to know this.
>
> That's apart from problems where A::Local is not a class;
> I suspect that most of those would be picked up during
> linking.  The access control problem would not be detected
> at all by most current compilers, as far as I know.

The access control problem won't be detected today by most compilers if
the programmer simply uses two definitions of class A:

class A {
public:
    struct Local { };
};

and

class A {
private:
    struct Local { };
};

There is a very large advantage to being able to forward declare a
nested class, since you can often avoid having to import the entire
declaration of the outer class.  This can greatly reduce the coupling
between modules and shorten compile time considerably.

I think it is very much worth the potential cost of a little bit of
access control safety, which is pretty trivial to break if the
programmer is determined anyway.

--
Jon Biggar
Floorboard Software
jon@floorboard.com
jon@biggar.org

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: "Pavel Kuznetsov" <pavel_kuznetsov@no.spam.users.sourceforge.net>
Date: Tue, 5 Jun 2001 09:57:54 GMT
Raw View
It would be nice if it was allowed to write something like:

class A;
class A::Local;
A::Local* p;

Is there any problem allowing that?

--
Pavel Kuznetsov
mailto:pavel_kuznetsov@no.spam.users.sourceforge.net
remove "no.spam." from e-mail address


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: pdimov@mmltd.net (Peter Dimov)
Date: Wed, 6 Jun 2001 20:17:26 GMT
Raw View
Jonathan Biggar <jon@floorboard.com> wrote in message news:<3B1D1FE7.D5AF0A66@floorboard.com>...

> There is a very large advantage to being able to forward declare a
> nested class, since you can often avoid having to import the entire
> declaration of the outer class.  This can greatly reduce the coupling
> between modules and shorten compile time considerably.

If you want to forward-declare a class, do not make it nested:

class A;
class A_local;

// use A_local

class A_local
{
};

class A
{
public:
  typedef A_local Local;
};

--
Peter Dimov
Multi Media Ltd.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]