Lecture overview -- Keyboard shortcut: 'u'  Previous page: Example of copying objects: Default copying -- Keyboard shortcut: 'p'  Next page: Preventing object copying -- Keyboard shortcut: 'n'  Lecture notes - all slides and notes together  slide -- Keyboard shortcut: 't'  Help page about these notes  Alphabetic index  Course home  Lecture 3 - Page 16 : 36
Notes about C++
Abstraction Mechanisms, Part 1
Example of copying objects: Programmed copying

We want to modify the meaning of copying during initialization and assignment

The point array of the line segment is copied by the copy constructor and by the assignment operator

The following versions of the program illustrates a typical pattern for copy constructors and assignment operators

  • The C++ Programming Language: Page 245-246
 

y:/Kurt/Files/Advanced-programming-cpp/cpp/kn/copying-objects/vers2/point-lineseg.hClass LineSegment WITH a copy constructor and and WITH an assignment operator.

Header file.

y:/Kurt/Files/Advanced-programming-cpp/cpp/kn/copying-objects/vers2/point-lineseg.ccThe corresponding cc file with implementations of the copy constructor and the assignment operator.


y:/Kurt/Files/Advanced-programming-cpp/cpp/kn/copying-objects/vers2/prog.ccA function that constructs, initializes and assigns LineSegments.


y:/Kurt/Files/Advanced-programming-cpp/cpp/kn/copying-objects/vers2/program-outputProgram execution.


  • Effective C++, Third edition: Item 10
 

The copy constructor makes a copy of of the point array

The assignment deallocates the point array of the lhs (which is overwritten) and reallocates space for the points in the new copy

The programs above follow the pattern from Stroustrup §10.4.4.1 page 245