home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!mcsun!news.funet.fi!ajk.tele.fi!funic!nokia.fi!newshost!girod
- From: girod@node_262d6.kiloapo.ts.tele.nokia.fi (Marc Girod)
- Subject: Implementation of MI
- Message-ID: <GIROD.92Dec31091627@node_262d6.kiloapo.ts.tele.nokia.fi>
- Sender: usenet@noknic.nokia.fi (USENET at noknic)
- Nntp-Posting-Host: rat.ts.tele.nokia.fi
- Reply-To: marc.girod@ntc.nokia.com
- Organization: kpd
- Distribution: comp
- Date: Thu, 31 Dec 1992 07:16:27 GMT
- Lines: 91
-
- Hi netters!
-
- In his book: 'C++ Programming Style', Tom Cargill suggests to test
- your compiler with the following example (Listing 9.14 p 219)
-
- -mi.C-----------------------------------------------------------
- #include <iostream.h>
-
- class Top {
- int x;
- public:
- Top();
- Top(const Top&);
- Top& operator=(const Top&);
- };
-
- Top::Top(): x(0) {
- cout << "\tTop::Top() this=" << this << "\n";
- }
-
- Top::Top(const Top& t): x(t.x) {
- cout << "\tTop::Top(const Top&) this=" << this << "\n";
- }
-
- Top& Top::operator=(const Top& rhs) {
- cout << "\tTop::operator=() this=" << this << "\n";
- if (this != &rhs)
- x = rhs.x;
- return *this;
- }
-
- class Left: public virtual Top {
- int y;
- };
-
- class Right: public virtual Top {
- int z;
- };
-
- class Bottom: public Left, public Right {
- };
-
- main() {
- cout << "Bottom first;\n";
- Bottom first;
- cout << "Bottom copy = first; \n";
- Bottom copy = first;
- cout << "copy = first;\n";
- copy = first;
- return 0;
- }
- -mi.C-end----------------------------------------------------------
-
- Well, compiled under HP-UX with CC v3.0, I get the following output:
-
- > mi
- Bottom first;
- Top::Top() this=0x68fad210
- Bottom copy = first;
- Top::Top(const Top&) this=0x68fad234
- copy = first;
- Top::operator=() this=0x68fad234
- Top::operator=() this=0x68fad234
- >
-
- ...where as the author expected, Top::operator= is erroneously called
- twice.
-
- With g++ v2.3.3, both under HP-UX and SunOs, I get:
-
- > mi
- Bottom first;
- Top::Top() this=0x68fad210
- Bottom copy = first;
- Top::Top() this=0x68fad228
- Top::Top(const Top&) this=0x68fad228
- copy = first;
- Top::operator=() this=0x68fad228
- >
-
- ...where this problem doesn't appear..., but why is the Top
- constructor called?
-
- Happy New Year!
- --
- +-----------------------------------------------------------------------------+
- | Marc Girod - Nokia Telecommunications Phone: +358-0-511 7703 |
- | TL4E - P.O. Box 12 Fax: +358-0-511 7432 |
- | SF-02611 Espoo 61 - Finland Internet: marc.girod@ntc.nokia.com |
- | X.400: C=FI, A=Elisa, P=Nokia Telecom, UNIT=TRS, SUR=Girod, GIV=Marc |
- +-----------------------------------------------------------------------------+
-