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

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!mcsun!news.funet.fi!ajk.tele.fi!funic!nokia.fi!newshost!girod
  3. From: girod@node_262d6.kiloapo.ts.tele.nokia.fi (Marc Girod)
  4. Subject: Implementation of MI
  5. Message-ID: <GIROD.92Dec31091627@node_262d6.kiloapo.ts.tele.nokia.fi>
  6. Sender: usenet@noknic.nokia.fi (USENET at noknic)
  7. Nntp-Posting-Host: rat.ts.tele.nokia.fi
  8. Reply-To: marc.girod@ntc.nokia.com
  9. Organization: kpd
  10. Distribution: comp
  11. Date: Thu, 31 Dec 1992 07:16:27 GMT
  12. Lines: 91
  13.  
  14. Hi netters!
  15.  
  16. In his book: 'C++ Programming Style', Tom Cargill suggests to test
  17. your compiler with the following example (Listing 9.14 p 219)
  18.  
  19. -mi.C-----------------------------------------------------------
  20. #include <iostream.h>
  21.  
  22. class Top {
  23.     int x;
  24.   public:
  25.     Top();
  26.     Top(const Top&);
  27.     Top& operator=(const Top&);
  28. };
  29.  
  30. Top::Top(): x(0) {
  31.     cout << "\tTop::Top() this=" << this << "\n";
  32. }
  33.  
  34. Top::Top(const Top& t): x(t.x) {
  35.     cout << "\tTop::Top(const Top&) this=" << this << "\n";
  36. }
  37.  
  38. Top& Top::operator=(const Top& rhs) {
  39.     cout << "\tTop::operator=() this=" << this << "\n";
  40.     if (this != &rhs)
  41.         x = rhs.x;
  42.     return *this;
  43. }
  44.  
  45. class Left: public virtual Top {
  46.     int y;
  47. };
  48.  
  49. class Right: public virtual Top {
  50.     int z;
  51. };
  52.  
  53. class Bottom: public Left, public Right {
  54. };
  55.  
  56. main() {
  57.     cout << "Bottom first;\n";
  58.     Bottom first;
  59.     cout << "Bottom copy = first; \n";
  60.     Bottom copy = first;
  61.     cout << "copy = first;\n";
  62.     copy = first;
  63.     return 0;
  64. }
  65. -mi.C-end----------------------------------------------------------
  66.  
  67. Well, compiled under HP-UX with CC v3.0, I get the following output:
  68.  
  69. > mi
  70. Bottom first;
  71.     Top::Top() this=0x68fad210
  72. Bottom copy = first; 
  73.     Top::Top(const Top&) this=0x68fad234
  74. copy = first;
  75.     Top::operator=() this=0x68fad234
  76.     Top::operator=() this=0x68fad234
  77. >
  78.  
  79. ...where as the author expected, Top::operator= is erroneously called
  80. twice.
  81.  
  82. With g++ v2.3.3, both under HP-UX and SunOs, I get:
  83.  
  84. > mi
  85. Bottom first;
  86.     Top::Top() this=0x68fad210
  87. Bottom copy = first; 
  88.     Top::Top() this=0x68fad228
  89.     Top::Top(const Top&) this=0x68fad228
  90. copy = first;
  91.     Top::operator=() this=0x68fad228
  92. >
  93.  
  94. ...where this problem doesn't appear..., but why is the Top
  95. constructor called?
  96.  
  97. Happy New Year!
  98. --
  99. +-----------------------------------------------------------------------------+
  100. | Marc Girod - Nokia Telecommunications       Phone: +358-0-511 7703          |
  101. | TL4E - P.O. Box 12                            Fax: +358-0-511 7432          |
  102. | SF-02611 Espoo 61 - Finland              Internet: marc.girod@ntc.nokia.com |
  103. |    X.400: C=FI, A=Elisa, P=Nokia Telecom, UNIT=TRS, SUR=Girod, GIV=Marc     |
  104. +-----------------------------------------------------------------------------+
  105.