home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / lang / cplus / 18360 < prev    next >
Encoding:
Text File  |  1992-12-23  |  2.6 KB  |  131 lines

  1. Path: sparky!uunet!pipex!bnr.co.uk!uknet!axion!gssec.bt.co.uk!mjackson
  2. From: mjackson@gssec.bt.co.uk (Mark Jackson)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: prototyping object?
  5. Message-ID: <1992Dec23.170803.19309@gssec.bt.co.uk>
  6. Date: 23 Dec 92 17:08:03 GMT
  7. Sender: usenet@gssec.bt.co.uk
  8. Followup-To: <1992Dec22.114524.421@ghost.dsi.unimi.it>
  9. Organization: BT, Software Engineering Centre, Glasgow, Scotland
  10. Lines: 119
  11.  
  12.  
  13. In article <1992Dec22.114524.421@ghost.dsi.unimi.it>,
  14.  valentm@ghost.dsi.unimi.it (marco valentini) writes:
  15. |> 
  16. |> I have written the following code:
  17. |> class paolo;  //!Is Right this ??????
  18. |> object pippo
  19. |> {
  20. |>   paolo* p;
  21. |>   public:
  22. |>    void ....
  23. |>    ..... (various methods)
  24. |> 
  25. |> }
  26. |> object paolo
  27. |> {
  28. |>   pippo* pi;
  29. |>   public:
  30. |>    int ...
  31. |>    ...(variuos methods)
  32. |> }
  33. |> 
  34.  
  35. Firstly, it is not recommended to have two classes mutually dependent on
  36. each other.
  37. i.e. pippo needs to know about paulo, and vice-versa. Check your design.
  38.  
  39. Secondly, there is nothing wrong with your 'forward declaration' of
  40. class paulo.
  41. What the compilation error sounds like is a module problem.
  42. Separate the implementation from the class interface. i.e.
  43.  
  44. in file 'pippo.h'------------------------
  45.  
  46. class paulo;    // Forward declaration. O.K.
  47.  
  48. class pippo
  49. {
  50.   paulo* p;
  51.  
  52.   public:
  53.     (interfaces to various methods)
  54.     void setPaulo(paulo* newP);
  55.     int usePaulo();
  56.     int doSomething();
  57. };
  58.  
  59. (end of 'pippo.h')------------------------
  60.  
  61. in file 'paulo.h'--------------------------
  62.  
  63. class pippo;    // Forward declaration. O.K.
  64.  
  65. class paulo
  66. {
  67.   pippo* pi;
  68.  
  69.   public:
  70.     (interfaces to various methods)
  71.     int setPippo(pippo* newPi);
  72.     int usePippo();
  73.     int doSomethingElse();
  74. };
  75.  
  76. (end of 'paulo.h')-------------------------
  77.  
  78. Then in file 'pippo.cc' (or whatever file extension you use)
  79.  
  80. #include "pippo.h"
  81. #include "paulo.h"  // This gives you access to paulo's methods
  82.  
  83. void pippo::setPaulo(paulo* newP)
  84. {
  85.   p = newP;
  86. }
  87.  
  88. int pippo::usePaulo()
  89. {
  90.   if( p != NULL )
  91.     return( p->doSomethingElse() );
  92.   else
  93.     return( -1 );
  94. }
  95.  
  96. (end of 'pippo.cc')-------------------------
  97.  
  98. Same thing for paulo.cc
  99.  
  100. #include "paulo.h"
  101. #include "pippo.h"  // This gives you access to pippo's methods
  102.  
  103. void paulo::setPippo(pippo* newP)
  104. {
  105.   pi = newP;
  106. }
  107.  
  108. int paulo::usePippo()
  109. {
  110.   if( pi != NULL )
  111.     return( pi->doSomething() );
  112.   else
  113.     return( -1 );
  114. }
  115.  
  116. (end of 'paulo.cc')-------------------------
  117.  
  118. Hope this helps, Merry Christmas &c.
  119.  
  120. Mark Jackson
  121.  
  122. -- 
  123.  
  124.    !!!!!!!     ___________________________________
  125.    +(.)(.)    (                                   )
  126.    C   > )    |  HO, HO. MERRY CHRISTMAS, NETTERS |
  127.     \ V / ----(___________________________________)
  128.      ---
  129.  
  130. P.S. I tried email, but got 'Service Unavailable'....
  131.