1 / 22

Chapter 15-3 Pointers, Dynamic Data, and Reference Types

Chapter 15-3 Pointers, Dynamic Data, and Reference Types. Dale/Weems. What happens. When a function is called that passes a DynArray object by value, what happens?. 2000. DynArray. ? ? 75 ? ?. Private: size 5 arr 2000. ~DynArray. DynArray. ValueAt. Store.

quin-king
Download Presentation

Chapter 15-3 Pointers, Dynamic Data, and Reference Types

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. Chapter 15-3Pointers, Dynamic Data, and Reference Types Dale/Weems

  2. What happens . . . • When a function is called that passes a DynArray object by value, what happens? 2000 DynArray ? ? 75 ? ? Private: size 5 arr 2000 ~DynArray DynArray ValueAt Store CopyFrom

  3. Passing a Class Object by Value // Function code void SomeFunc(DynArray someArr) // Uses pass by value { . . . . } 3

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

  5. SupposeSomeFunccalls Store void SomeFunc(DynArray someArr) // Uses pass by value { someArr.Store(290, 2); . . . } What happens in the shallow copy scenario? 5

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

  7. beta.arr[2] has changed Although beta is passed by value, its dynamic data has changed! beta someArr 2000 DynArray . . . DynArray . . . ? ? 290 ? ? Private: size 5 arr 2000 Private: size 5 arr 2000 shallow copy

  8. Shallow Copyvs.Deep Copy • A shallow copycopies only the class data members, and does not make a copy of any pointed-to data • Adeep copycopies not only the class data members, but also makes a separate stored copy of any pointed-to data

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

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

  11. Initialization of Class Objects • C++ defines initialization to mean • initialization in a variable declaration • passing an object argument by value • returning an object as the return value of a function • By default, C++ uses shallow copies for these initializations

  12. As a result . . . • When a class has a data member that points to dynamically allocated data, you must write what is called acopy constructor • The copy constructoris implicitly called in initialization situationsandmakes a deep copy of the dynamic data in a different memory location

  13. More about Copy Constructors • When you provide (write) a copy constructor for a class, the copy constructor is used to make copies for pass by value • You do not explicitly 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

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

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

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

  17. 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 occurs { 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 } 17

  18. 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 that points to dynamic data, you should write a member function to create a deep copy of the dynamic data

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

  20. 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-] // 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]; } 20

  21. Appointment Calendar Day • Insert art on page 824

  22. The End of Chapter 15 Part 3

More Related