From: "Jesper Lund" Newsgroups: comp.os.msdos.djgpp References: <003201c13a3f$053b2f40$aa7ba8c0 AT fuego> Subject: Re: Can You help me about djgpp with c++ Lines: 71 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.00.2615.200 X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2615.200 Message-ID: <5Bbn7.6249$9V5.360511@news000.worldonline.dk> Date: Tue, 11 Sep 2001 00:58:08 +0200 NNTP-Posting-Host: 213.237.1.108 X-Complaints-To: news-abuse AT wol DOT dk X-Trace: news000.worldonline.dk 1000162753 213.237.1.108 (Tue, 11 Sep 2001 00:59:13 MET DST) NNTP-Posting-Date: Tue, 11 Sep 2001 00:59:13 MET DST Organization: Customer of Tiscali A/S (World Online) To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com Gorden wrote in message news:003201c13a3f$053b2f40$aa7ba8c0 AT fuego... > I Buy a book (Teach Yourself C++, 3rd Edition By Herbert Schildt) inside > have a C++ smaple > > #include > #include > > class samp > { > private: > int i; > > public: > samp(void) { cout << "constructor" << '\n'; } > ~samp(void) { cout << "destructor " << '\n'; } > void set(int n) { i = n ; } > int get(void ) { return i; } > > }; > > int test(samp o) > { > return o.get() * o.get(); > } > > void main(void) > { > clrscr(); > > samp o; > o.set(10); > cout << test(o) << '\n'; > } > > The Book have result is > constructor > destructor > 100 > destructor > > in borland c++ 3.01 the result is > constructor > destructor > 100 > destructor > > But in djgpp the result is > constructor > destructor > 100 > destructor > destructor > > Why in djgpp 3 times call destructor, I try compile on borland C++ 3.01 have > 2 times call destructor > Is djgpp or gpp error ?????????? > Apparently, gcc makes TWO copies of your samp object when it is passed as a value argument to the test(samp o) function (and Borland only one). I don't think that the C++ standard guarantees that only one copy of your object is made in this situation (but I could be wrong here?). By the way, it seems that if you define your own copy constructor (instead of relying on the one generated implicitly by the C++ compiler), only one copy of the samp object is made.