From: DavMac AT iname DOT com (Davin McCall) Newsgroups: comp.os.msdos.djgpp Subject: Re: no copy-ctor for temps Date: Sat, 02 Oct 1999 01:53:32 GMT Organization: Monash Uni Lines: 59 Distribution: world Message-ID: <37f564ff.3448616@newsserver.cc.monash.edu.au> References: NNTP-Posting-Host: damcc5.halls.monash.edu.au X-Trace: towncrier.cc.monash.edu.au 938829209 19168 130.194.198.138 (2 Oct 1999 01:53:29 GMT) X-Complaints-To: abuse AT monash DOT edu DOT au NNTP-Posting-Date: 2 Oct 1999 01:53:29 GMT X-Newsreader: Forte Free Agent 1.1/32.230 To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com On Fri, 01 Oct 1999 19:45:14 GMT, wemccaug AT prairienet DOT org (Wendy E. McCaughrin) wrote: > The following example shows that 'gxx' will not invoke a copy-ctor > for temporaries, only for variables. Are you saying that's bad? The code you point out as not calling the copy-ctor instantiates a temporary instance of the class as a function parameter. The reason that the copy-ctor is not called is because no copy operation is performed - the temporary object is created directly on the stack before the function is called. The alternative, to construct the object and then copy it to the stack, would be less efficient. I am aware that this sort of thing can get confusing - I've been caught out by things like this myself. Davin. > >#include >#include > > class Overflow > { char mssg[80]; > public: > Overflow( char* ccp ) { strcpy(mssg,ccp); } > Overflow( const Overflow& ovfl ) // must be 'const' ! > { cerr << "copy ctor: "; strcpy(mssg,ovfl.mssg); } > void Report() { cerr << mssg; } > }; > > void TstCpy( Overflow ); // call by value > > int main() > { Overflow of = " I am a variable\n"; > TstCpy(of); // passing a variable: copy-ctor invoked > TstCpy(Overflow(" I am a temporary\n")); // passing temp: no copy-ctor > return 0; > } > > > void TstCpy(Overflow ovrflw) > { ovrflw.Report(); } > > > When compiled and run, the output is: > > copy ctor: I am a variable (indicating call of copy-ctor) > I am a temporary (defaults to bit-wise copy) __________________________________________________________ *** davmac - sharkin'!! davmac AT iname DOT com *** my programming page: http://yoyo.cc.monash.edu.au/~davmac/