delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/10/02/01:56:51

From: wemccaug AT prairienet DOT org (Wendy E. McCaughrin)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: no copy-ctor for temps
References: <pafJ3.1246$%K6 DOT 47 AT firefly> <37F562D4 DOT 57291365 AT a DOT crl DOT com> <eb8J3.1214$%K6 DOT 36 AT firefly>
Lines: 86
Message-ID: <CDfJ3.1252$%K6.15@firefly>
Date: Sat, 02 Oct 1999 04:13:22 GMT
NNTP-Posting-Host: 192.17.3.4
X-Complaints-To: newsmgr AT prairienet DOT org
X-Trace: firefly 938837602 192.17.3.4 (Fri, 01 Oct 1999 23:13:22 CDT)
NNTP-Posting-Date: Fri, 01 Oct 1999 23:13:22 CDT
Organization: Prairienet -- Your Community Network for East Central Illinois
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

In a previous article, wemccaug AT prairienet DOT org (Wendy E. McCaughrin) says:

>
>In a previous article, weiqigao AT a DOT crl DOT com (Weiqi Gao) says:
>
>>"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.
>>
> 2 questions:
>
> 1. Your reference for the above assertion?
> 2. You say that the new object is merely constructed at the place
>    where temp is allocated -- how is this different from bit-wise
>    copy? By "construct" do you mean the char* ctor is invoked to
>    build 'temp' -- then I agree it is not bit-wise copy. But you
>    express it like assignment, which defaults to bit-wise copy.
>
 I might add the following from the ARM (Section 5.2.2 Function Call):
 "When a function is called, each formal argument is initialized with 
 its actual argument."
 (Where Stroustrup says "formal argument", I have said "parameter" above)
 The word "assignment" does not appear in this statement.

 I tried operator= earlier, but it was never invoked for passing anything.
 

 

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019