Presentation is loading. Please wait.

Presentation is loading. Please wait.

The Big Three Based on Weiss “Data Structures and algorithm Analysis CS240 Computer Science II.

Similar presentations


Presentation on theme: "The Big Three Based on Weiss “Data Structures and algorithm Analysis CS240 Computer Science II."— Presentation transcript:

1 The Big Three Based on Weiss “Data Structures and algorithm Analysis CS240 Computer Science II

2 The Big Three The destructor The copy constructor The operator= If the does not supply his own, the default version of all three functions provided by the system will be used.

3 The Destructor called whenever an object goes out of scope. free up resources that were allocated during the use of the object.

4 Copy Constructor construct a new object that is initialized to a copy of the same type of object. a copy constructor is called when –datatype a = b;// b already defined datatype a(b);// euqivalent to above Note that a = b; // not the same as above –an object is passed using call by value. –an object returned by value.

5 operator= a = b; assignment operator = is called when one existing object is assigned (in a member- wise fashion) to another existing object.

6 Problems with the defaults The problem occurs when one or more data members of a class are pointer type. –the default destructor for pointers does not de- allocate the dynamically allocated object pointed to by the pointer! –both the copy constructor and operator= only copy the value (address) of the pointer instead of the object pointer to by the pointer. Thus, we will have two class instances that contain pointers pointing to the same object. This is known as the shallow copy; normally one would expect a deep copy where a clone of the the entire object is made.

7 Why the defaults do not work: an example class intCell { public: intCell(int initVal = 0){p = new int(initVal);} int read( ) const {return *p;} void set(int x) {*p = x;} private: int *p; };

8 Why the defaults do not work: an example continued int f() { intCell a(2); intCell b = a;// calls the default copy constructor intCell c; c = b;// calls the default operator= a.set(4); cout << a.read() << ‘\t’ << b.read() << ‘\t’ << a.read() << endl; return 0; } What are the output? Do you expect 4 2 2? It turns out that the outputs are 4 4 4! Why?

9 The memory leak problem The content of data member p of c object is replaced by that of b object, thus the int object pointed to by p of c is no longer accessible for deletion, resulting in a problems known as memory leak.

10 The fix: programmer supplies the big three class intCell { public: intCell(int initVal = 0) {p = new int(initVal);} intCell(const intCell & rhs);// copy constructor ~intCell( );// destructor const intCell& operator=(const intCell & rhs); int read( ) const {return *p;} void set(int x) {*p = x;} private: int *p; };

11 The fix: programmer supplied copy constructor and destructor intCell::intCell(const intCell & rhs) { p = new int(*rhs.p);// rhs is an intCell object }// contents of pointee copied // instead address of pointee // is copied! intCell::~intCell( ) { delete p; }

12 The fix: programmer supplied operator= cosnt intCell & intCell::operator=(cosnt intCell & rhs) { if (this != &rhs)// avoid copy to self *p = *rhs.p;// rhs is an intCell object return *this; } Note again: the contents of the pointee is copied instead of the address of pointee is copied!


Download ppt "The Big Three Based on Weiss “Data Structures and algorithm Analysis CS240 Computer Science II."

Similar presentations


Ads by Google