home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / lang / cplus / 15919 < prev    next >
Encoding:
Internet Message Format  |  1992-11-08  |  2.1 KB

  1. 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
  2. From: mccauleyba@vax1.bham.ac.uk (Brian McCauley)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Class scope and class declarations
  5. Message-ID: <1992Nov7.163916.1@vax1.bham.ac.uk>
  6. Date: 7 Nov 92 16:39:16 GMT
  7. References: <JEP.92Nov6155424@thneed.cs.duke.edu>
  8. Sender: usenet@rs6000.bham.ac.uk (USENET News Service)
  9. Organization: University of Birmingham
  10. Lines: 50
  11.  
  12. In article <JEP.92Nov6155424@thneed.cs.duke.edu>, jep@cs.duke.edu (Jonathan E. Polito) writes:
  13. > It is clear that:
  14. > class X;
  15. > class Y {
  16. > public:
  17. >     Y();
  18. > private:
  19. >     class X* xptr;
  20. > };
  21. > class X {
  22. > public:
  23. >     int info;
  24. > };
  25. > Y::Y() {
  26. >     X tmp;
  27. >     tmp.info = 1;
  28. > }
  29. > will cause tmp to be defined as a class X (at file scope) object. What
  30. > if the first class X; forward declaration is deleted though? Is the
  31. > effect then that xptr in class Y is a pointer to a local class X that
  32. > is never defined? This is one interpretation though I have seen a
  33. > number of programs that rely on class X* to be a forward declaration
  34. > to a class defined later at file scope.
  35. It seems like only yesterday I answered this same question in comp.std.c++ -
  36. hey hang on it *was* yesterday.
  37.  
  38. If you say `class X;' this is a forward declaration to a class defined later.
  39. If you say `X *xptr;' this referres to a class X that has already been
  40. declared (and must be in scope).
  41.  
  42. If you use the hybrid form `class X *xptr;' this will be compiled as
  43. `X *xptr;' if there is a class X in scope otherwise it will be compiled as 
  44. `class X; X *xptr'. (IMHO it is best to avoid the hybrid form.)
  45.  
  46. In C++ Ed.1 all classes had file scope so old software that relied on the
  47. fact that there was no such thing as nested class scopes will not compile
  48. correctly under a C++ Ed.2 compiler.
  49.  
  50. To fix old code it is usually suffient to insert forward declarations to
  51. all classes at the top of the header. 
  52.     \\   ( )  NO BULLSHIT! from BAM (Brian McCauley)          
  53.  .  _\\__[oo  ============
  54. .__/  \\ /\@
  55. .  l___\\     E-mail: B.A.McCauley@bham.ac.uk
  56.  # ll  l\\ 
  57. ###LL  LL\\
  58.