home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / lang / cplus / 19896 < prev    next >
Encoding:
Text File  |  1993-01-25  |  3.0 KB  |  101 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!zaphod.mps.ohio-state.edu!menudo.uh.edu!lobster!nuchat!moe
  3. From: moe@nuchat.sccsi.com (Norman C. Kluksdahl)
  4. Subject: comp.sys.ibm.pc comp.sys.ibm.pc.programmer comp.sys.ibm.pc.misc
  5. Message-ID: <1993Jan25.214958.29671@nuchat.sccsi.com>
  6. Summary: cpp class interactions; programming problem
  7. Organization: South Coast Computing Services, Inc.
  8. Date: Mon, 25 Jan 1993 21:49:58 GMT
  9. Lines: 90
  10.  
  11. I have a small (large) problem with instances of my classes recognizing
  12. each other.  I have enclosed code snippets to exemplify the problem:
  13.  
  14.     I have a main, which has the pointers to the class instances as
  15. global variables.  Two types of class instances are used:  a screen handler
  16. and an array of dcsystem objects.  The main program examines the input
  17. file to determine how many dcsystem instances to create, then assignes
  18. the pointer to the array of objects.  The objects are initialized, which
  19. happens correctly, then the screen handler is invoked, which will handle
  20. all screen updates and input events, and will invoke the correct method of
  21. the dcsystem instances.
  22.  
  23.     The problem is that, while the main loop calls the correct object of the
  24. dcsystem array, the screen handler gets horribly lost.  As a debug, i checked
  25. the pointer to the dcsystem array, and it is badly lost.
  26.  
  27. The code is compiled with GCC on a 486 PC.
  28.  
  29. (NOTE:  the code sample below is typed in a hurry, so it may not be precisely
  30. syntactically correct, but it does compile and parts work without error.)
  31.  
  32. Any suggestions for further debugging or views into what I may have done
  33. wrong would be greatly appreciated.  Please E-mail responses.
  34.  
  35. ###########   CODE SAMPLE    ####################
  36.  
  37. Dcsys * dcsystem;
  38. Screen * screen_hdlr;
  39.  
  40. int    numsystems;
  41.  
  42. main
  43. {
  44.     ***  get the number of systems from the input  ***
  45.     dcsystem = new Dcsys[numsystems];
  46.  
  47.     for ( int i = 0; i<numsystems; i++)
  48.         dcsystem[i].init();
  49.  
  50.     *** more initialization stuff ***
  51.  
  52.     screen_hdlr.init();
  53.  
  54.     *** by this point, the whole system is screwed up ***
  55. }
  56.  
  57. ***** dcsystem.cc
  58.  
  59. #include screen.h    //  definitions for the screen class    
  60.  
  61. extern Dcsys *dcsystem;
  62. extern Screen *screen_hdlr;
  63.  
  64. Dcsys
  65. {
  66.     *** misc class variables ***
  67. public:
  68.     void    init(void);
  69.     void    dump(void);        //  debug module to dump 'this' and values 
  70.                             //  of class variables
  71.     void    drawsys(void);    //  module to draw the given instance
  72. }
  73. //  methods to do init, dump, and drawsys follow.  None of these use
  74. //  the screen methods
  75.  
  76. *****  screen.cc
  77.  
  78. #include dcsys.h    // definitions for the dcsystem class
  79.  
  80. extern Dcsys *dcsystem;
  81. extern Screen *screen_hdlr;
  82.  
  83. Screen
  84.     {
  85.     **** misc class variables ***
  86. public:
  87.     void    init(void);        // this sucker causes no problems; it just sets
  88.                             // up the screen
  89.     void    drawsys(int i);    //  this is the problem
  90. }
  91.  
  92. Screen::drawsys(int i)
  93. {
  94.     *** misc setup, all involving screen_hdlr's variables and other methods
  95.  
  96.     dcsystem[i].drawsys();        // this causes seg violations
  97. }
  98.  
  99. **********************************************************************
  100. N. Kluksdahl        moe@nuchat.sccsi.com
  101.