home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / gnu / g / help / 1204 < prev    next >
Encoding:
Text File  |  1992-09-08  |  2.3 KB  |  103 lines

  1. Path: sparky!uunet!haven.umd.edu!darwin.sura.net!seismo!brennan
  2. From: brennan@seismo.CSS.GOV (Mary Ann Brennan)
  3. Newsgroups: gnu.g++.help
  4. Subject: using cout in a constructor (Q)
  5. Message-ID: <51201@seismo.CSS.GOV>
  6. Date: 8 Sep 92 20:37:39 GMT
  7. Sender: usenet@seismo.CSS.GOV
  8. Organization: Center for Seismic Studies, Arlington, VA
  9. Lines: 91
  10. Nntp-Posting-Host: tenso.css.gov
  11.  
  12.  
  13. Apologies for the length, I couldn't find any other postings that helped me.
  14. I'm trying to teach myself C++ and am reading Stroustrup's
  15. "The C++ Programming Language" Reprinted with corrections July 1987.
  16.  
  17. On p. 167 exercise 12 is:
  18. Given the program:
  19.  
  20. #include <stream.h>
  21.  
  22. main()
  23. {
  24.         cout << "Hello, world\n" ;
  25. }
  26.  
  27. modify it to produce the output
  28.  
  29. Initialize
  30. Hello, world
  31. Clean up
  32.  
  33. Do not change main() in any way.
  34.  
  35. on pp. 158-9 Stroustrup discusses static store and suggests inventing
  36. a type with a constructor and destructor for initialization and cleanup
  37. to be used only once to allocate a static object so the constructor
  38. and destructor are called.
  39.  
  40. my first attempt used streams as follows:
  41.  
  42. // startend1.h
  43.  
  44. class startend {
  45.         int *junk;
  46. public:
  47.         startend() {  junk = new int; cout << "Initialize\n"; }
  48.         ~startend() { delete junk; cout << "Clean up\n"; }
  49. };
  50.  
  51. // hello1.c
  52. #include <stream.h>
  53. #include "startend1.h"
  54.  
  55. static startend bookkeep;
  56.  
  57. main()
  58. {
  59.         cout << "Hello, world\n" ;
  60. }
  61.  
  62. it had a segmentation fault (it appeared to die while running the constructor
  63. and that cout may not have been initialized before used -- surmised by stepping
  64. through with adb, dbx didn't seem to like g++).
  65.  
  66. After fiddling with it for a bit, I gave up on streams and changed to stdio:
  67.  
  68. // startend2.h
  69.  
  70. class startend {
  71.         int *junk;
  72. public:
  73.         startend() {  junk = new int; printf("Initialize\n"); }
  74.         ~startend() { delete junk; printf("Clean up\n"); }
  75. };
  76.  
  77. // hello2.c
  78. #include <stream.h>
  79. #include "startend2.h"
  80.  
  81. static startend bookkeep;
  82.  
  83. main()
  84. {
  85.         cout << "Hello, world\n" ;
  86. }
  87.  
  88. which produced the intended result without core dumping:
  89.  
  90. Initialize
  91. Hello, world
  92. Clean up
  93.  
  94. my questions:
  95. Why doesn't the first version work?
  96. Is there any way to use streams inside a constructor?
  97. More generally, what do I seem to be misunderstanding?
  98.  
  99. Thanks in advance,
  100. Mary Ann Brennan
  101.  
  102. brennan@seismo.css.gov
  103.