Topic: copy vs assignment


Author: Peter Lawrence <peterl@scruznet.com>
Date: 1996/06/18
Raw View
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.
Thanks, Pete Lawrence (peterl@scruznet.com)


[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: kanze@lts.sel.alcatel.de (James Kanze US/ESC 60/3/141 #40763)
Date: 1996/06/18
Raw View
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
]





Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 1996/06/19
Raw View
In article 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.

The difference is between initialization and assignment. In C, there
isn't much practical difference, although there is a theoretical
difference. In C++ the difference is important.

Initialization is the giving of a value to an object as it is newly
created.

Assignment is the giving of a value to an object that already existed.

int j = 1; // initialize new object 'j' with the value 1
int k;     // create a new object 'k' with no explicit initialization
k = 2;     // assign the value 2 to 'k', which already existed

Initialization is always accomplished by a constructor, and constructors
are not used for any other purpose. For some type T:
1. T t1 = 0; // create a new T and initialize it with 0.
2. T t2;     // create a new T with no explicit initialization
3. t2 = t1;  // assign the value of t1 to t2
4. T t3 = t1; // create a new T and initialize it with the value of t1
5. T t3(t1); // another way to write the previous line

#1 uses T::T(0), assuming that is valid
#2 uses T::T(), assuming that is valid
#3 uses T::operator=(const T&), which will be created if not declared
#4 uses T::T(const T&), which will be created if not declared
#5 is another way to write #4

(I over-simplified about the "const" in the above examples, but let's
not get into that.)

The semantics of initialization and assignment for a type are often
the same. Sometimes they must be different. Simple example:
 class X { int& ir; ... };
When an X is created, the reference member must be initialized to
refer to some int object. Once an X object exists, there is no valid
way to make member ir refer to any other object. The details of the copy
constructor and any assignment operator cannot be identical.
 X::X(int& j) : ir(j) { ... } // must initialize ir
 X& X::operator=(const X& x) { ... } // cannot initialize ir

Now suppose an object contained a pointer to itself. You would initialize
that pointer with "this" in any constructor. Any assignment operator
would have to be sure not to change the value of that pointer. (You
don't want the object's own pointer to point to another object, but
always to itself.)
---
Steve Clamage, stephen.clamage@eng.sun.com
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]