copying-objects/vers1-a/prog.cc - A function that constructs, initializes and assigns LineSegments. | Lecture 4 - slide 19 : 40 Program 3 |
// A program that constructs LineSegments, copies them, and assignmens them. // Problems occur when the LineSegment objects are destructed. #include <iostream> #include "point-lineseg.h" using namespace std; void h(){ LineSegment ls1, // LineSegment(10) constructed ls2 = ls1, // copy initialization ls3; // LineSegment(10) constructed ls3 = ls2; // copy assignment - ls3 is now a copy of ls2. // Here all LineSegments ls1, ls2 and ls3 refer to the same Point array // because the Point pointers are just copied in the // initialization and the assignment shown above. // Destructors called for ls1, ls2, ls3 on exit from h. // Trouble for the two last destructions. We delete the linesegment on the free store more than once! } int main(){ h(); }