Lecture overview -- Keyboard shortcut: 'u'  Previous page: Copying Point objects in parameter passing -- Keyboard shortcut: 'p'  Next page: Example of copying objects: Programmed 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 15 : 36
Notes about C++
Abstraction Mechanisms, Part 1
Example of copying objects: Default copying

Motivates the need for copy constructors and assignment overloading in C++

The starting point is automatic variables of class type (value semantics)

Class LineSegment has a pointer to the first Point in an array

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

y:/Kurt/Files/Advanced-programming-cpp/cpp/kn/copying-objects/vers1/point-lineseg.hClass LineSegment WITHOUT copy constructor and and WITHOUT assignment operator.

Header file.

y:/Kurt/Files/Advanced-programming-cpp/cpp/kn/copying-objects/vers1/point-lineseg.ccThe corresponding cc file - not particularly relevant.


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


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


The problem is that the first destruction of a LineSegment object destructs (deallocates) the shared Point array in the free store

The two next destructions encounter dangling references

The program is improved on the next slide