home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!haven.umd.edu!darwin.sura.net!spool.mu.edu!agate!doc.ic.ac.uk!uknet!news.cs.bham.ac.uk!bham!bhamvx!mccauleyba
- From: mccauleyba@vax1.bham.ac.uk (Brian McCauley)
- Newsgroups: comp.lang.c++
- Subject: Re: Class scope and class declarations
- Message-ID: <1992Nov7.163916.1@vax1.bham.ac.uk>
- Date: 7 Nov 92 16:39:16 GMT
- References: <JEP.92Nov6155424@thneed.cs.duke.edu>
- Sender: usenet@rs6000.bham.ac.uk (USENET News Service)
- Organization: University of Birmingham
- Lines: 50
-
- In article <JEP.92Nov6155424@thneed.cs.duke.edu>, jep@cs.duke.edu (Jonathan E. Polito) writes:
- > It is clear that:
- > class X;
- >
- > class Y {
- > public:
- > Y();
- > private:
- > class X* xptr;
- > };
- >
- > class X {
- > public:
- > int info;
- > };
- >
- > Y::Y() {
- > X tmp;
- > tmp.info = 1;
- > }
- >
- > will cause tmp to be defined as a class X (at file scope) object. What
- > if the first class X; forward declaration is deleted though? Is the
- > effect then that xptr in class Y is a pointer to a local class X that
- > is never defined? This is one interpretation though I have seen a
- > number of programs that rely on class X* to be a forward declaration
- > to a class defined later at file scope.
- It seems like only yesterday I answered this same question in comp.std.c++ -
- hey hang on it *was* yesterday.
-
- If you say `class X;' this is a forward declaration to a class defined later.
- If you say `X *xptr;' this referres to a class X that has already been
- declared (and must be in scope).
-
- If you use the hybrid form `class X *xptr;' this will be compiled as
- `X *xptr;' if there is a class X in scope otherwise it will be compiled as
- `class X; X *xptr'. (IMHO it is best to avoid the hybrid form.)
-
- In C++ Ed.1 all classes had file scope so old software that relied on the
- fact that there was no such thing as nested class scopes will not compile
- correctly under a C++ Ed.2 compiler.
-
- To fix old code it is usually suffient to insert forward declarations to
- all classes at the top of the header.
- \\ ( ) NO BULLSHIT! from BAM (Brian McCauley)
- . _\\__[oo ============
- .__/ \\ /\@
- . l___\\ E-mail: B.A.McCauley@bham.ac.uk
- # ll l\\
- ###LL LL\\
-