Automatics Copy Constructor and Assignment Operator Andy Wang

  • Slides: 24
Download presentation
Automatics, Copy Constructor, and Assignment Operator Andy Wang Object Oriented Programming in C++ COP

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

Automatic Functions In C++, some default functions are automatically built Constructor // if you

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=

Copy Constructor Just like a constructor Invoked implicitly when a COPY of an existing

Copy Constructor Just like a constructor 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 f 1, f 2(3, 4); Fraction f 3 = f 2; // a copy of f 2 is created to initialize f 3 f 1 = f 2; // calls the assignment operator, since f 1 and f 2 // already exist

Declaring and Defining A copy constructor creates a new object, initialized as a copy

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 class. Name(const class. Name &); The const is not required, but a good idea for protecting the original Example Fraction(const Fraction &f);

Shallow vs. Deep Copy The default version makes a shallow copy Each member data

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 entry. List pointer will be copied and will point to the original dynamically allocated memory

Deep Copy Creates a copy of dynamically allocated memory Directory: : Directory(const Directory &d)

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

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

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

Visualize the Execution stack (high address) int main() { Directory d; … main int

Visualize the Execution stack (high address) int main() { Directory d; … main int max. Size = 5 int curent. Size = 0 Entry *entry. List = Entry[5] heap (low address) d

Shallow Copy stack (high address) int main() { main Directory d; Directory d 2

Shallow Copy stack (high address) int main() { main Directory d; Directory d 2 = d; int max. Size = 5 int curent. Size = 0 Entry *entry. List = Entry[5] heap (low address) d

Problems of Shallow Copy Potential inconsistencies Non dynamically allocated variables are copied Dynamically allocated

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

Deep Copy stack (high address) int main() { main Directory d; Directory d 2

Deep Copy stack (high address) int main() { main Directory d; Directory d 2 = d; int max. Size = 5 int curent. Size = 0 Entry *entry. List = Entry[5] heap (low address) d

Assignment Operator operator= is similar to the copy constructor Called when one object is

Assignment Operator operator= is similar to the copy constructor Called when one object is assigned to another Fraction f 1, f 2; f 1 = f 2; 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

Copy Constructor vs. Assignment Operator Copy Constructor Initializes a new object as a copy

Copy Constructor vs. Assignment Operator Copy Constructor Initializes a new object as a copy of an existing one Initialize data for the first time Has no return value Operator= 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

Chained Assignments a = b = c = 4; Same as a = (b

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

The this pointer Inside a member function An object can access its own address

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 &);

Directory Operator= Implementation Directory &Directory: : operator=(const Directory &d) { if (this == &d)

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 [] entry. List; maxsize = d. maxsize; currentsize = d. currentsize; entry. List = new Entry[d. maxsize]; for (int j = 0; j < currentsize; j++) entry. List[j] = d. entry. List[j]; return *this; }

Phonebook Example http: //www. cs. fsu. edu/~myers/cop 3330/examples/copyconst /phonebook/

Phonebook Example http: //www. cs. fsu. edu/~myers/cop 3330/examples/copyconst /phonebook/

directory. h #include “entry. h” class Directory { public: … Directory(const Directory &); Directory&

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

directory. cpp … Directory: : Directory(const Directory &d) { maxsize = d. maxsize; currentsize

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

directory. cpp Directory &Directory: : operator=(const Directory &d) { if (this == &d) return

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

menu. cpp … Int main() { Directory d; d. Insert(); Directory d 2 =

menu. cpp … Int main() { Directory d; d. Insert(); Directory d 2 = d; // copy constructor d. Display. Directory(); d 2. Display. Directory; d. Update(); d. Display. Directory(); d 2. Display. Directory; }

Fraction Example http: //www. cs. fsu. edu/~myers/cop 3330/examples/copyconst /frac/ No need for copy constructor

Fraction Example http: //www. cs. fsu. edu/~myers/cop 3330/examples/copyconst /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

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

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

frac. cpp … Fraction: : Fraction(const Fraction &f) { numerator = f. numerator; denominator

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