1 / 24

Copy Constructor

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.

Download Presentation

Copy Constructor

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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.

  2. 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.

  3. 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

  4. Passing a Class Object by Value // FUNCTION CODE void SomeFunc( DynArray someArr ) // Uses pass by value { . . . . } 17

  5. 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

  6. SupposeSomeFunccalls Store // FUNCTION CODE void SomeFunc( DynArray someArr ) // Uses pass by value { someArr.Store(290, 2); . . . } WHAT HAPPENS IN THE SHALLOW COPY SCENARIO? 19

  7. 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

  8. 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

  9. Classes with Data Member Pointers Need CONSTRUCTOR COPY CONSTRUCTOR DESTRUCTOR

  10. 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

  11. 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

  12. 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

  13. Assignment and Initialization • In both cases the value of one object is given to another. • Copy constructor only applies to initializations.

  14. 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

  15. 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.

  16. 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.

  17. 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

  18. 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

  19. 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

  20. 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

  21. 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

  22. 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

  23. 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

  24. 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

More Related