Topic: About Copy constructors
Author: Geir.Halbo@ugle.unit.no (Geir Halbo)
Date: 26 Jul 93 02:00:54 Raw View
karim@von-neumann (Aktouf Karim) asks what a copy constructor is.
Followup set to comp.lang.c++, as comp.std.c++ is about c++ *standards.*
In article <22hghmINNfei@medicine.wustl.edu> reece@informatics.WUSTL.EDU (Reece Kimball Hart) writes:
>
> Karim-
> First, consider the assigment
> A = B;
> where A and B are members of the same class.
> In C++, the compiler just performs a bit-copy of the structure of B onto A.
> If the class contains no pointers, then that's great. The net effect will be
> that A.v == B.v, where v is any variable in the structure, which is what the
> programmer meant (presumably) by the statement "A = B;".
>
> However, what happens when the class contains a reference to allocated
> memory? That is, suppose the class constructor allocate a block of memory
> and stores the pointer in a class variable. Now, when the bit-copy is
> performed we get two objects which each point to the same chunk of allocated
> memory. It is unlikely that this was the intended result. In order to
> override this effect, a copy constructor can be used to modify the behavior
> of an assignment; in this case, we might wish to allocate a new block of the
> same size and copy the contents of the block referenced in B to a /new/ block
> now referenced in A.
Still not quite right. A copy constructor is used at
*initialization*, which is distinct from assignment.
int n = 3;
int n(3);
are equivalent declarations initializating the n integer from a 3 integer,
whereas
int n;
n = 3; // note difference from int n = 3;
is a declaration (without initialization) followed by an assignment to n.
This distinction is important, as different user-defined functions are called.
Initialization occurs when an object is created, which includes
argument and return value passing and more.
An *overloaded assignment operator* is used at assignment.
class string {
char *contents;
...
string(string& c) { copy c to this at init of this from c }
string& operator=(string& c) { copy c to this at assign of c to this }
};
string a;
string b(a); // init, copy constructor string::string(a) called
string c = a; // still init, same call
string d;
d = a; // assign, assign operator d.operator=(a) called
So if I have understood this correctly, to make sure that a new copy
of the string *contents* (rather than just the pointer) is made
always, *both* copy constructor *and* assignment operator must be user
defined, and there exists no way to define both at once.
- geir
--
MSc Geir Halbo ! Department of Computer Systems and Telematics (IDT)
Halbo@idt.unit.no ! Norwegian Institute of Technology (NTH)
+ 47 7 59 36 74 ! University of Trondheim (UNIT)
G=Geir; S=Halbo; OU=IDT; O=Unit; P=uninett; C=no
Author: karim@von-neumann (Aktouf Karim)
Date: Mon, 19 Jul 1993 21:24:50 GMT Raw View
Hi,
I'm relatively new to the C++ world, and I don't understant exactly
what's the meaning of a copy constructor... Why sometines it's needed
and sometimes not? I know that it has something to do with temporary
and a secure way to keep them until they're not needed anymore or
to prevent destroying them too soon... But I'm really not comfortable
with that concept... I've read some books on it, but didn't find a
satisfying answer to my questions...
Thanks in advance!
Karim
--
| Irikar Le Maure | Ecole : karim@info.polymtl.ca |
| Je pense, donc je nuis | Boulot: karim@alex.qc.ca |
|----------------------------------------------------------|
| Ecole Polytechnique de Montreal - Alex Informatique Inc. |
.. il n'entrerait dans la saintete, aux yeux de Dieu, que
s'il rentrait dans l'infamie aux yeux des hommes.
Victor Hugo, "Les miserables".
Author: reece@informatics.WUSTL.EDU (Reece Kimball Hart)
Date: 20 Jul 1993 19:20:22 GMT Raw View
karim@von-neumann (Aktouf Karim) writes:
>Hi,
>I'm relatively new to the C++ world, and I don't understant exactly
>what's the meaning of a copy constructor... Why sometines it's needed
>and sometimes not? I know that it has something to do with temporary
>and a secure way to keep them until they're not needed anymore or
>to prevent destroying them too soon... But I'm really not comfortable
>with that concept... I've read some books on it, but didn't find a
>satisfying answer to my questions...
>Thanks in advance!
>Karim
>--
> | Irikar Le Maure | Ecole : karim@info.polymtl.ca |
> | Je pense, donc je nuis | Boulot: karim@alex.qc.ca |
> |----------------------------------------------------------|
> | Ecole Polytechnique de Montreal - Alex Informatique Inc. |
> .. il n'entrerait dans la saintete, aux yeux de Dieu, que
> s'il rentrait dans l'infamie aux yeux des hommes.
> Victor Hugo, "Les miserables".
Karim-
First, consider the assigment
A = B;
where A and B are members of the same class.
In C++, the compiler just performs a bit-copy of the structure of B onto A.
If the class contains no pointers, then that's great. The net effect will be
that A.v == B.v, where v is any variable in the structure, which is what the
programmer meant (presumably) by the statement "A = B;".
However, what happens when the class contains a reference to allocated
memory? That is, suppose the class constructor allocate a block of memory
and stores the pointer in a class variable. Now, when the bit-copy is
performed we get two objects which each point to the same chunk of allocated
memory. It is unlikely that this was the intended result. In order to
override this effect, a copy constructor can be used to modify the behavior
of an assignment; in this case, we might wish to allocate a new block of the
same size and copy the contents of the block referenced in B to a /new/ block
now referenced in A.
I hope this helps
Reece
Author: b91926@fnclub.fnal.gov (David Sachs)
Date: 21 Jul 1993 15:50:07 GMT Raw View
In article <22hghmINNfei@medicine.wustl.edu>, reece@informatics.WUSTL.EDU (Reece Kimball Hart) writes:
|> karim@von-neumann (Aktouf Karim) writes:
|> >Hi,
|> >I'm relatively new to the C++ world, and I don't understant exactly
|> >what's the meaning of a copy constructor... Why sometines it's needed
|> >and sometimes not? I know that it has something to do with temporary
|> >and a secure way to keep them until they're not needed anymore or
|> >to prevent destroying them too soon... But I'm really not comfortable
|> >with that concept... I've read some books on it, but didn't find a
|> >satisfying answer to my questions...
|>
|> >Thanks in advance!
|>
|> >Karim
|> >--
|> > | Irikar Le Maure | Ecole : karim@info.polymtl.ca |
|> > | Je pense, donc je nuis | Boulot: karim@alex.qc.ca |
|> > |----------------------------------------------------------|
|> > | Ecole Polytechnique de Montreal - Alex Informatique Inc. |
|>
|> > .. il n'entrerait dans la saintete, aux yeux de Dieu, que
|> > s'il rentrait dans l'infamie aux yeux des hommes.
|> > Victor Hugo, "Les miserables".
|>
|>
|> Karim-
|> First, consider the assigment
|> A = B;
|> where A and B are members of the same class.
|> In C++, the compiler just performs a bit-copy of the structure of B onto A.
|> If the class contains no pointers, then that's great. The net effect will be
|> that A.v == B.v, where v is any variable in the structure, which is what the
|> programmer meant (presumably) by the statement "A = B;".
|>
|> However, what happens when the class contains a reference to allocated
|> memory? That is, suppose the class constructor allocate a block of memory
|> and stores the pointer in a class variable. Now, when the bit-copy is
|> performed we get two objects which each point to the same chunk of allocated
|> memory. It is unlikely that this was the intended result. In order to
|> override this effect, a copy constructor can be used to modify the behavior
|> of an assignment; in this case, we might wish to allocate a new block of the
|> same size and copy the contents of the block referenced in B to a /new/ block
|> now referenced in A.
|>
|> I hope this helps
|> Reece
|>
The copy constructor is NOT used in this case, because A must be an already initialized object.
The simplest case in which the copy constructor is used is a declaration with an initializer. e.g. if the class name is X, and B is an existing object of type X, then a declaration of the form:
X A=B;
would use the copy constructor for class X.
A simple assignment statement (A=B where A and B are objects of type X) uses another poorly understood function, X::operator=(&X)
Author: swf@tools3teradata.com (Stan Friesen)
Date: 21 Jul 93 22:07:29 GMT Raw View
In article <22hghmINNfei@medicine.wustl.edu>, reece@informatics.WUSTL.EDU (Reece Kimball Hart) writes:
|>
|> Karim-
|> First, consider the assigment
|> A = B;
|> where A and B are members of the same class.
|> In C++, the compiler just performs a bit-copy of the structure of B onto A.
Not quite right, actually it does a *member-wise* copy.
If all of the data members of the class are built-in types,
or have no assignment operator, then the two are equivalent.
But is any data member is of a class type that has an assignment
operator, that assignment operator is used for that member.
--
sarima@teradata.com (formerly tdatirv!sarima)
or
Stanley.Friesen@ElSegundoCA.ncr.com
The peace of God be with you.