Mail Archives: djgpp/1999/10/01/23:05:20
From: | Weiqi Gao <weiqigao AT a DOT crl DOT com>
|
Newsgroups: | comp.os.msdos.djgpp
|
Subject: | Re: no copy-ctor for temps
|
Date: | Fri, 01 Oct 1999 20:41:40 -0500
|
Organization: | CRL Network Services
|
Lines: | 64
|
Message-ID: | <37F562D4.57291365@a.crl.com>
|
References: | <eb8J3.1214$%K6 DOT 36 AT firefly>
|
NNTP-Posting-Host: | a116022.stl1.as.crl.com
|
Mime-Version: | 1.0
|
X-Mailer: | Mozilla 4.51 [en] (X11; I; Linux 2.2.5-15 i586)
|
X-Accept-Language: | en
|
To: | djgpp AT delorie DOT com
|
DJ-Gateway: | from newsgroup comp.os.msdos.djgpp
|
Reply-To: | djgpp AT delorie DOT com
|
"Wendy E. McCaughrin" wrote:
>
> The following example shows that 'gxx' will not invoke a copy-ctor
> for temporaries, only for variables.
There's no need to.
> #include <iostream.h>
> #include <string.h>
>
> 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
The object is constructed on TstCpy()'s stack. Remember that argument
passing is like initialization. Therefore
TstCpy(of);
is akin to
Overflow temp = of;
which invokes the copy constructor, whereas
TstCpy(Overflow(" I an a temporary\n"));
is akin to
Overflow temp = Overflow(" I an a temporary\n");
which does not invoke the copy constructor, but merely constructs the
new object at the place where temp is allocated.
> 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)
It's not a bit-wise copy after all.
--
Weiqi Gao
weiqigao AT a DOT crl DOT com
- Raw text -