511 likes | 1.44k Views
Copy Constructor. An overloaded constructor When an object is passed to a function, a bitwise (exact) copy of that object is made and given to the function. If the object contains a pointer to allocated memory, the copy will point to the memory as does the original object. Copy Constructor.
E N D
Copy Constructor • An overloaded constructor • When an object is passed to a function, a bitwise (exact) copy of that object is made and given to the function. • If the object contains a pointer to allocated memory, the copy will point to the memory as does the original object.
Copy Constructor • If the copy makes a change to the contents of this memory, it will be changed for the original object too. • Also, when the function terminates, the copy will be destroyed, causing its destructor to be called. • That can free dynamically allocated memory, used by the original object as well.
What happens . . . • When a function is called that uses pass by value for a class object of DynArray type? 2000 DynArray ? ? 75 ? ? Private: size 5 arr 2000 ~DynArray DynArray ValueAt Store CopyFrom
Passing a Class Object by Value // FUNCTION CODE void SomeFunc( DynArray someArr ) // Uses pass by value { . . . . } 17
By default,Pass-by-value makes a shallow copy DynArray beta(5); // CLIENT CODE . . . SomeFunc( beta ); // function call beta someArr 2000 DynArray . . . DynArray . . . ? ? 75 ? ? Private: size 5 arr 2000 Private: size 5 arr 2000 shallow copy
SupposeSomeFunccalls Store // FUNCTION CODE void SomeFunc( DynArray someArr ) // Uses pass by value { someArr.Store(290, 2); . . . } WHAT HAPPENS IN THE SHALLOW COPY SCENARIO? 19
beta.arr[2] has changed DynArray beta(5); // CLIENT CODE . . . SomeFunc( beta); beta someArr 2000 DynArray . . . DynArray . . . ? ? 290 ? ? Private: size 5 arr 2000 Private: size 5 arr 2000 shallow copy
beta.arr[2] has changed NOTICE THAT NOT JUST FOR THE SHALLOW COPY, BUT ALSO FOR ARGUMENT beta, THE DYNAMIC DATA HAS CHANGED! beta someArr 2000 DynArray . . . DynArray . . . ? ? 290 ? ? Private: size 5 arr 2000 Private: size 5 arr 2000 shallow copy
Classes with Data Member Pointers Need CONSTRUCTOR COPY CONSTRUCTOR DESTRUCTOR
Shallow Copyvs.Deep Copy • a shallow copycopies only the class data members, and does not make a copy of any pointed-to data • a deep copycopies not only the class data members, but also makes a separate stored copy of any pointed-to data
What’s the difference? • a shallow copyshares the pointed to dynamic data with the original class object • a deep copymakes its own copy of the pointed to dynamic data at different locations than the original class object
Making a (Separate) Deep Copy beta someArr 2000 DynArray . . . ? ? 75 ? ? Private: size 5 arr 2000 4000 DynArray . . . ? ? 75 ? ? Private: size 5 arr 4000 deep copy
Assignment and Initialization • In both cases the value of one object is given to another. • Copy constructor only applies to initializations.
Initialization of Class Objects • Initialization can occur three ways: • an object is used to initialize another in a declaration statement, • passing an object argument by value to a function, • returning a temporary object as the return value of a function, • by default, C++ uses shallow copies for these initializations
default copy constructor • C++ automatically provides a default copy constructor that simply duplicates the object. • It is possible to specify precisely how one object will initialize another by defining a copy constructor. • Copy constructor do not affect assignment operations.
Common form of Copy Constructor classname(const classname &obj) { ... } • Here obj is a reference to an object that is being used to initialize another object. Time t1=t2; // explicitly initializing t1 func1(t2); // t2 passed as a parameter t2=func1(); // t2 receiving a returned object In the first two cases, a reference to t2 would be passed to the copy constructor. In the third, a reference to the object returned by funct2() is passed to the copy constructor.
As a result . . . • when a class has a data member pointer to dynamically allocated data, you should write what is called acopy constructor • the copy constructor is implicitly called in initialization situationsandmakes a deep copy of the dynamic data in a different memory location
More about Copy Constructors • when there is a copy constructor provided for a class, the copy constructor is used to make copies for pass by value • you do not call the copy constructor • like other constructors, it has no return type • because the copy constructor properly defines pass by value for your class, it must use pass by reference in its definition
Copy Constructor • copy constructor is a special member function of a class that is implicitly called in these 3 situations: • passing object parameters by value • initializing an object variable in its declaration • returning an object as the return value of a function
SomeFunc(beta);// copy-constructor // beta passed by value beta someArr 2000 4000 DynArray DynArray ? ? 75 ? ? ? ? 75 ? ? Private: size 5 arr 2000 Private: size 5 arr 4000 ~DynArray ~DynArray DynArray DynArray ValueAt ValueAt Store Store CopyFrom CopyFrom DEEP COPY
DynArray::DynArray( const DynArray& otherArr ) // Copy constructor // Implicitly called for deep copy in initializations. // POST: If room on free store THEN // new array of size otherArr.size is created // on free store && arr == its base address // && size == otherArr.size // && arr[0..size-1] == otherArr.arr[0..size-1] // ELSE error message. { int i ; size = otherArr.size ; arr = new int[size] ; // allocate memory for copy for ( i = 0; i< size ; i++ ) arr[i] = otherArr.arr[i] ; // copies array } 34
What about the assignment operator? • the default method used for assignment of class objects makes a shallow copy • if your class has a data member pointer to dynamic data, you should write a member functionto create a deep copy of the dynamic data
gamma.CopyFrom(beta); gamma beta 3000 2000 DynArray DynArray ? ? 75 ? ? ? ? 75 ? ? Private: size 5 arr 3000 Private: size 5 arr 2000 ~DynArray ~DynArray DynArray DynArray ValueAt ValueAt Store Store CopyFrom CopyFrom DEEP COPY
void DynArray::CopyFrom ( /* in */ DynArray otherArr ) // Creates a deep copy of otherArr. // POST: Array pointed to by arr@entry deallocated // && IF room on free store // THEN new array is created on free store // && arr == its base address // && size == otherArr.size // && arr[0..size-1] == otherArr[0..size-1] // ELSE halts with error message. { int i ; delete [ ] arr ; // delete current array size = otherArr.size ; arr = new int [size] ; // allocate new array for ( i = 0; i< size ; i++ ) // deep copy array arr[i] = otherArr.arr[i] ; } 37