www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/10/01/18:29:31

From: wemccaug AT prairienet DOT org (Wendy E. McCaughrin)
Newsgroups: comp.os.msdos.djgpp
Subject: no copy-ctor for temps
Lines: 34
Message-ID: <eb8J3.1214$%K6.36@firefly>
Date: Fri, 01 Oct 1999 19:45:14 GMT
NNTP-Posting-Host: 192.17.3.4
X-Complaints-To: newsmgr AT prairienet DOT org
X-Trace: firefly 938807114 192.17.3.4 (Fri, 01 Oct 1999 14:45:14 CDT)
NNTP-Posting-Date: Fri, 01 Oct 1999 14:45:14 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

 The following example shows that 'gxx' will not invoke a copy-ctor
 for temporaries, only for variables.

#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
   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)

- Raw text -


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