Everything2
Near Matches
Ignore Exact
Full Text
Everything2

copy constructor

created by ariels

(thing) by ariels (2 d) (print)   ?   (I like it!) Sun Mar 04 2001 at 12:02:47

In C++, the copy constructor for a class X is the constructor member function X::X(const X& x). Its job is to create a copy of the object x passed in. Copying is performed when passing objects by value.

Note that the argument of the copy constructor must be passed by reference. The copy constructor is a function; if its argument were passed by value, the compiler would have to call the copy constructor to pass the copy constructor's argument, which would have to call the copy constructor, etc.

For simple objects the default copy constructor supplied by the compiler is good enough. Some authorities recommend writing a copy constructor in any case, but Bjarne Stroustrup recommends against doing so. The copy constructor may be used to implement various kinds of data sharing -- or to prevent Bad Things from happening because of any sharing induced by the default copy constructor.

If you find yourself in need of a copy constructor, remember the rule of three!


(thing) by PaynenDiaz (1.6 hr) (print)   ?   (I like it!) Fri Nov 16 2001 at 0:39:13

Copy constructor (noun) - the member function called automatically to create a new object that is a copy of an object whose copy constructor is called.

Alright, for everyone who is not me, I apologize and will clean up that sentence a bit. (Disclaimer: all information contained in this writeup is the way things work in C++, so if your favorite Object Oriented language does it differently, it is not my concern right now). When an object is copied, its copy constructor is called. This happens when you pass an object by VALUE. In pass by reference, a reference (or pointer) to the object is passed to the function, so no new objects are created on the run-time heap.

The default copy constructor for an object performs a shallow copy of all data members into the new object. This is fine if your class contains simple integers for example. But, if your class contains pointers, only the address of the pointers is copied to the new object. The most common use for a copy constructor is to perform a deep copy. A deep copy is when you manually create new instances of all the anonymous heap variables your class uses.


printable version
chaos

default copy constructor C++: brackets to make an object not a function rule of three Call by reference
C++ Conversion Constructor Constructor auto_ptr Why C++ doesn't suck
Assignable shallow copy Johnny Cash with His Hot and Blue Guitar Call by value
C++ The C++ Programming Language Freakshow How to unintentionally steal an air-to-air missile postincrement
The Borland Delphi compiler March 6, 2000 Technology that gets lost between now and Star Trek October 3, 2001
type-checking
Y'know, if you log in, you can write something here, or contact authors directly on the site. Create a New User if you don't already have an account.
  Epicenter
Login
Password

password reminder
register

Everything2 Help

Cool Staff Picks
Just another sprinkling of indeterminacy
Breeders: your children do not make you superior
The game is afoot, Watson!
Dearest, I feel certain
Christian Fundamentalism
Zazen
Mike Tyson
Paul Newman
Clinical depression
Der Ring des Nibelungen
September 3, 2005
Independence Day, Malcolm X and the Passover Seder
Simone Weil
The Alice in Wonderland Project
New Writeups
Augustine
Vanya on 42nd Street(review)
tentative
Chances Not Taken(idea)
Heitah
Why I love Everything2(person)
trixingee
Dungeon Mastering for the first time(idea)
Netrat0
It's Called Subtext, Honey(person)
eyeofthebeholder
The Dragon(idea)
Heitah
consist, comprise, constitute, or compose(idea)
Meezzio
Gotlandssnus(thing)
argv
Astral Plane(idea)
Madara
One Winged Angel(fiction)
Tom Rook
Talk is cheap(poetry)
shaogo
Adelle Davis(person)
Aerobe
race car g sfjsgsd(poetry)
Binah
Dream Log: July 5, 2008(dream)
StrawberryFrog
Forgotten things in space(idea)
E2 is a by-product of the existence of The Everything Development Company