From 6126205325129175759
X-Google-Language: ENGLISH,ASCII
X-Google-Thread: f78e5,f35f303f52b43897
X-Google-Attributes: gidf78e5,public
From: kanze@lts.sel.alcatel.de (James Kanze US/ESC 60/3/141 #40763)
Subject: Re: copy vs assignment
Date: 1996/06/18
Message-ID: <KANZE.96Jun18194308@slsvgqt.lts.sel.alcatel.de>#1/1
X-Deja-AN: 160887950
references: <31C6B570.388A@scruznet.com>
x-original-date: 18 Jun 1996 17:43:08 GMT
apparently-to: std-c++@ncar.UCAR.EDU
organization: GABI Software, Sarl.
x-auth: PGPMoose V1.1 PGP comp.std.c++
newsgroups: comp.std.c++
originator: austern@isolde.mti.sgi.com


In article <31C6B570.388A@scruznet.com> Peter Lawrence
<peterl@scruznet.com> writes:

|> What is the difference between a (compiler generated default) copy
|> constructor and an assignment operator. Is it just that the interface
|> is different, one operates on "this" and the other operates on an
|> explicit reference. When does the compiler implicitly use copy verses
|> assignement, and why. Do people ever make the semantics of copy and
|> assignment different, and why.

This is, or should be, in the C++ FAQ.  (It also doesn't have much to
do with the C++ standardization effort.)

Basically, the compiler uses a constructor (including the copy
constructor) to construct raw memory into an object.  A constructor
works on uninitialized memory.

The assignment operator is used to assign one value to another already
initialized object.  An assignment operator expects to find a fully
initialized object to work on.

Think about an object which has a dynamically allocated pointer.  The
assignment operator might delete the pointer, then allocate new memory
(of a different size).  A constructor (copy or other) dare not delete
the pointer, since when the constructor is called, the memory has not
been initialized, and contains random bit patterns.

See Scott Meyers, "Effective C++" for more details.
-- 
James Kanze         Tel.: (+33) 88 14 49 00        email: kanze@gabi-soft.fr
GABI Software, Sarl., 8 rue des Francs-Bourgeois, F-67000 Strasbourg, France
Conseils, �tudes et r�alisations en logiciel orient� objet --
                -- A la recherche d'une activit� dans une region francophone
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your 
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu 
]



