Presentation is loading. Please wait.

Presentation is loading. Please wait.

Automatics, Copy Constructor, and Assignment Operator

Similar presentations


Presentation on theme: "Automatics, Copy Constructor, and Assignment Operator"— Presentation transcript:

1 Automatics, Copy Constructor, and Assignment Operator
Andy Wang Object Oriented Programming in C++ COP 3330

2 Automatic Functions In C++, some default functions are automatically built Constructor // if you don’t provide one Destructor // if you don’t provide one Copy Constructor Assignment operator=

3 Copy Constructor Just like a constructor Examples
Invoked implicitly when a COPY of an existing object is created; in particular, when An object is defined to have the value of another object of the same type An object is passed by value into function An object is returned (by value) from a function Examples Fraction f1, f2(3,4); Fraction f3 = f2; // a copy of f2 is created to initialize f3 f1 = f2; // calls the assignment operator, since f1 and f2 // already exist

4 Declaring and Defining
A copy constructor creates a new object, initialized as a copy of another existing object Always has one parameter of the same type Passed by reference Since passing by value will invoke a copy constructor Format className(const className &); The const is not required, but a good idea for protecting the original Example Fraction(const Fraction &f);

5 Shallow vs. Deep Copy The default version makes a shallow copy
Each member data location is copied Sufficient for classes like Fraction Only has a private numerator and denominator May not be sufficient for pointer member data Example: Phonebook The entryList pointer will be copied and will point to the original dynamically allocated memory

6 Deep Copy Creates a copy of dynamically allocated memory
Directory::Directory(const Directory &d) { maxsize = d.maxsize; currentsize = d.currentsize; entryList = new Entry[d.maxsize]; for (int j = 0; j < currentsize; j++) entryList[j] = d.entryList[j]; }

7 Visualize the Execution
stack (high address) int main() { … main heap (low address)

8 Visualize the Execution
stack (high address) int main() { Directory d; … main int maxSize = 5 int curentSize = 0 Entry *entryList = d Entry[5] heap (low address)

9 Shallow Copy stack (high address)
int main() { Directory d; Directory d2 = d; main int maxSize = 5 int curentSize = 0 Entry *entryList = d int maxSize = 5 int curentSize = 0 Entry *entryList = Entry[5] heap (low address)

10 Problems of Shallow Copy
Potential inconsistencies Non dynamically allocated variables are copied Dynamically allocated variables point to the same memory region Double free Runtime error

11 Deep Copy stack (high address)
int main() { Directory d; Directory d2 = d; main int maxSize = 5 int curentSize = 0 Entry *entryList = d int maxSize = 5 int curentSize = 0 Entry *entryList = Entry[5] Entry[5] heap (low address)

12 Assignment Operator operator= is similar to the copy constructor
Called when one object is assigned to another Fraction f1, f2; f1 = f2; Assignment operator also has to make a copy The default version makes a shallow copy A deep copy needs to be overloaded by the user

13 Copy Constructor vs. Assignment Operator
Initializes a new object as a copy of an existing one Initialize data for the first time Has no return value Sets the current state of an object to that of another existing object May need to free up old dynamically allocated memory Returns the value that was assigned, to support a = b = c = 4; Must be a member function

14 Chained Assignments a = b = c = 4; Same as a = (b = (c = 4));
Thus, (c = 4) returns 4 by reference Need the ability to refer to an object from inside the object

15 The this pointer Inside a member function
An object can access its own address using the this keyword To return the object itself by reference, return the target of the this pointer, or *this Like a copy constructor, operator= will take one parameter of the same object type Examples Directory &operator=(const Directory &); Fraction &operator=(const Fraction &);

16 Directory Operator= Implementation
Directory &Directory::operator=(const Directory &d) { if (this == &d) return *this; // don’t self copy // if you don’t check this, this can self destruct… delete [] entryList; maxsize = d.maxsize; currentsize = d.currentsize; entryList = new Entry[d.maxsize]; for (int j = 0; j < currentsize; j++) entryList[j] = d.entryList[j]; return *this; }

17 Phonebook Example pyconst/phonebook/

18 directory.h #include “entry.h” class Directory { public: … Directory(const Directory &); Directory& operator=(const Directory &); private: };

19 directory.cpp … Directory::Directory(const Directory &d) { maxsize = d.maxsize; currentsize = d.currentsize; entryList = new Entry[d.maxsize]; for (int j = 0; j < currentsize; j++) entryList[j] = d.entryList[j]; }

20 directory.cpp Directory &Directory::operator=(const Directory &d) { if (this == &d) return *this; // don’t self copy delete [] entryList; maxsize = d.maxsize; currentsize = d.currentsize; entryList = new Entry[d.maxsize]; for (int j = 0; j < currentsize; j++) entryList[j] = d.entryList[j]; return *this; } …

21 menu.cpp … Int main() { Directory d; d.Insert(); Directory d2 = d; // copy constructor d.DisplayDirectory(); d2.DisplayDirectory; d.Update(); }

22 Fraction Example pyconst/frac/ No need for copy constructor and operator= Fraction does not have dynamically allocated memory Automatic versions are sufficient with the use of shallow copies

23 frac.h class Fraction { public: … Fraction(const Fraction &f); Fraction &operator=(const Fraction &f); private: };

24 frac.cpp … Fraction::Fraction(const Fraction &f) { numerator = f.numerator; denominator = f.denominator; } Fraction &Fraction::operator=(const Fraction &f) { return *this;


Download ppt "Automatics, Copy Constructor, and Assignment Operator"

Similar presentations


Ads by Google