Copy Constructors and Overloaded Assignment

Slides:



Advertisements
Similar presentations
Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Advertisements

Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
F UNCTION O VERLOADING Chapter 5 Department of CSE, BUET 1.
Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
OOP Languages: Java vs C++
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
CS212: Object Oriented Analysis and Design Lecture 12: Operator Overloading-II.
CMSC 202 Lesson 14 Copy and Assignment. Warmup Write the constructor for the following class: class CellPhone { public: CellPhone(int number, const string&
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
CMSC 202, Version 3/02 1 Copy Constructors and Overloaded Assignment.
OOP in C++ CS 124. Program Structure C++ Program: collection of files Source (.cpp) files be compiled separately to be linked into an executable Files.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
More about Java Classes Writing your own Java Classes More about constructors and creating objects.
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
1 // SPECIFICATION FILE (dynarray.h) // Safe integer array class allows run-time specification // of size, prevents indexes from going out of bounds, //
1 Ugly Realities The Dark Side of C++ Chapter 12.
@ George Wolberg, CSC212 Data Structure Lecture 6 Dynamic Classes and the Law of the Big Three Instructor: George Wolberg Department of Computer.
On dynamic memory and classes ● We previously had only discussed dynamic memory in regards to structs and dynamic arrays. ● However, they can be used (to.
Memory Management.
Yan Shi CS/SE 2630 Lecture Notes
Pointer to an Object Can define a pointer to an object:
Learning Objectives Pointers as dada members
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Default Constructors A default constructor is a constructor that takes no arguments. If you write a class with no constructor at all, C++ will write a.
Andy Wang Object Oriented Programming in C++ COP 3330
Java Programming: Guided Learning with Early Objects
Introduction to Classes
Lesson 14 Copy and Assignment
Class: Special Topics Copy Constructors Static members Friends this
Memberwise Assignment / Initialization
This pointer, Dynamic memory allocation, Constructors and Destructor
The dirty secrets of objects
Dynamic Memory Review what is static, automatic, dynamic variables? why are dynamic(ally allocated) variables needed what is program stack? function.
CMSC 202 Lesson 21 Exceptions II.
Dynamically Allocated Memory
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Object Oriented Programming COP3330 / CGS5409
Classes with Dynamically Allocated Data
Introduction to Classes
Chapter 15 Pointers, Dynamic Data, and Reference Types
Automatics, Copy Constructor, and Assignment Operator
CMSC 202 Templates.
Exceptions 2 CMSC 202.
Chapter 15 Pointers, Dynamic Data, and Reference Types
Copy Assignment CSCE 121 J. Michael Moore.
Operator overloading Dr. Bhargavi Goswami
Chapter 15-3 Pointers, Dynamic Data, and Reference Types
CS148 Introduction to Programming II
9-10 Classes: A Deeper Look.
CSC212 Data Structure - Section RS
COP 3330 Object-oriented Programming in C++
A Deeper Look at Classes
Class: Special Topics 2 For classes using memory allocation
CS 144 Advanced C++ Programming March 21 Class Meeting
Templates CMSC 202, Version 4/02.
Copy Assignment CSCE 121.
Destructors, Copy Constructors & Copy Assignment Operators
Rule of Three Part 1 & 2.
Destructors, Copy Constructors & Copy Assignment Operators
CS 144 Advanced C++ Programming April 30 Class Meeting
9-10 Classes: A Deeper Look.
Classes and Objects Object Creation
SPL – PS3 C++ Classes.
CMSC 202 Constructors Version 9/10.
Presentation transcript:

Copy Constructors and Overloaded Assignment CMSC 202, Version 3/02

When do we make copies of an object? 1) When passing them to a function by value 2) When returning them from a function by value 3) When creating a new object that is initialized with a copy of an existing object 4) When assigning objects (x = y) Items 1, 2 and 3 are handled by the copy constructor. Item 4 is handled by overloading the assignment ( = ) operator. CMSC 202, Version 3/02

What is a copy constructor? It’s a constructor – it’s used to construct new objects It does so by making a copy of an existing object We can do so explicitly // construct t1 Time t1 (12, 34, 56); // construct t2 by copying t1 Time t2 (t1); // construct t3 by copying t1 Time t3 = t1; The compiler may make copies when it needs them CMSC 202, Version 3/02

Copy constructor syntax The function prototype for every copy constructor is of the form: ClassName::ClassName (const ClassName &); Why is it necessary to for this parameter to be passed by reference? Why can’t it be passed by value? CMSC 202, Version 3/02

Why haven’t we seen this before? The compiler provides a default copy constructor which up until now has been sufficient. The default copy constructor simply copies each of the data members from the existing object into the new object This is not sufficient if one or more of the data members points to dynamically allocated memory CMSC 202, Version 3/02

Dynamic Memory Within a Class Sometimes a data member of a class points to dynamically allocated memory. When this occurs, we have to answer the following questions When will the dynamic memory be allocated? When will the dynamic memory be deallocated? What else is affected? CMSC 202, Version 3/02

A potential security problem In more complex projects, you will often have pointers as private class members. It is critical that a copy constructor allocate new memory for each pointer in the new copy The default copy constructor will just create a new pointer for the copy and give it the same value as the original pointer. So the copy and original will both point to the same data. This is a gross semantic error: changing the copy will change the original! How can this create a serious security problem? CMSC 202, Version 3/02

A potential security problem Also, consider the “accessor” functions for your class, which exist simply to report the value of unchangeable private data members. Why is it a security problem if your function returns a pointer to the data instead of a copy of the data? This can be a hard error to catch in languages like Java, which blur the distinctions of which variables are pointers and which are not. These are security problems, and debugging nightmares. Be careful to avoid them. CMSC 202, Version 3/02

A Simple Array Class One class that is often defined in C++ applications and libraries is a “smart” array. It has features that the built-in array doesn’t have such as automatic initialization and automatically checking indices to prevent a core dump. Other features are also possible. We’ll use a such an array class to illustrate the impact of dynamic memory allocation within a class. CMSC 202, Version 3/02

SmartArray class SmartArray { public: SmartArray ( int size = 100 ); // other members private: int m_size; int *m_theData; }; CMSC 202, Version 3/02

Using SmartArray Some SmartArray objects: SmartArray a1 (50); // 50 ints SmartArray a2 (200); // 200 ints SmartArray a3; // 100 ints by default CMSC 202, Version 3/02

When Does the Memory Get Allocated? The obvious answer to this question is, “In the constructor.” SmartArray::SmartArray (int size) { m_size = size; m_theData = new int [ m_size]; for (int j = 0; j < m_size; j++) m_theData [ j ] = 0; } CMSC 202, Version 3/02

A Picture of Memory . . . Given the instantiation SmartArray a1(50); we get this picture of memory: a1: 50 m_size . . . m_theData 0 1 2 47 48 49 CMSC 202, Version 3/02

When Does the Memory Get Deallocated? The intuitive answer is, “In the destructor.” The compiler provides us with a default destructor that deallocates the private data members. But this is not sufficient. If we relied on the default destructor, we’d create a memory leak because the memory pointed to by m_theData would not be freed. SmartArray::~SmartArray ( ) { delete [ ] m_theData; } CMSC 202, Version 3/02

What Else Is Affected? This time the answer is not so obvious. Consider the problems we want to avoid with dynamic memory: dynamically allocated memory to which multiple things point (a probable logic error) dynamically allocated memory to which nothing points (a memory leak) a pointer that points “nowhere” (dangling pointer) All of these situations may arise when we wish to make a copy of a SmartArray object. CMSC 202, Version 3/02

Effect of Default Copy Constructor (shallow copy) 50 m_size 0 1 2 47 48 49 . . . m_theData 5 -1 6 19 2 -5 Because the default copy constructor only copied the contents of m_theData in a1 into m_theData in a2, both point to the array allocated in a1. (Possible dangling pointer or other logic problem!) a2: 50 m_size m_theData CMSC 202, Version 3/02

The picture of memory we want (deep copy) 50 m_size 0 1 2 47 48 49 . . . m_theData 5 -1 6 19 2 -5 a2: 50 m_size 0 1 2 47 48 49 . . . m_theData 5 -1 6 19 2 -5 CMSC 202, Version 3/02

SmartArray Copy Constructor SmartArray (const SmartArray& array) { m_size = array.m_size; m_theData = new int [ m_size ]; for (int j = 0; j < m_size; j++ ) m_theData[j] = array.m_theData [j]; } CMSC 202, Version 3/02

When Is the Copy Constructor Invoked? Silently by the compiler when we Pass by value: void someFunction(SmartArray array); Return by value: SmartArray someFunction(parameters) { SmartArray temp; // code manipulating “temp” return (temp); } CMSC 202, Version 3/02

When Is the Copy Constructor Invoked? (cont’d) Explicitly by us upon construction SmartArray a1; // constructing a2 as a copy of a1 SmartArray a2 = a1; OR SmartArray a2(a1); CMSC 202, Version 3/02

What’s an assignment operator? The assignment operator is the function operator= It’s called when we assign one existing object to another existing object Time t1 (12, 34, 56); Time t2; t2 = t1; // object assignment CMSC 202, Version 3/02

Why haven’t we heard of this before? (this may sound familiar) The compiler provides a default assignment operator, which up until now has been sufficient. The default assignment operator simply copies each of the data members from the existing object on the right hand side into the existing object on the left hand side. This is not sufficient if one or more of the data members points to dynamically allocated memory CMSC 202, Version 3/02

Assigning SmartArray Objects Consider the following code: SmartArray a1 ( 50 ); SmartArray a2 ( 50 ); // some code to manipulate a1 // some code to manipulate a2 // now assign a1 to a2 a2 = a1; CMSC 202, Version 3/02

Assignment Operator The statement a2 = a1; calls the assignment operator ( operator= ) for the SmartArray class. If we don’t provide operator=, the compiler uses the default behavior Like the default copy constructor, the default assignment operator does a member-by-member (shallow) assignment. CMSC 202, Version 3/02

Default Assignment Code Conceptually, the default assignment operator for SmartArray contains the following code m_size = rhs.m_size; m_theData = rhs.m_theData; CMSC 202, Version 3/02

Prior To Assignment . . . . . . We have a picture something like this: 50 m_size 0 1 2 47 48 49 . . . m_theData 4 12 40 -3 a2: a2: 50 m_size 0 1 2 47 48 49 . . . 5 42 6 58 m_theData CMSC 202, Version 3/02

After Default Assignment 50 m_size 0 1 2 47 48 49 . . . m_theData 4 12 40 -3 Because the default assignment operator only copied the contents of m_theData in a1 into m_theData in a2, both point to the array allocated in a1. a2: 50 m_size m_theData CMSC 202, Version 3/02

In Fact, It’s Worse What happened to the memory that a2 used to point to? We’ve also caused a memory leak. 0 1 2 47 48 49 . . . 5 42 6 58 CMSC 202, Version 3/02

We want this picture without a memory leak 50 m_size 0 1 2 47 48 49 . . . m_theData 4 12 40 -3 a2: 50 m_size 0 1 2 47 48 49 . . . m_theData 4 12 40 -3 CMSC 202, Version 3/02

SmartArray::operator= (A First Attempt) void SmartArray::operator= (const SmartArray& rhs) { // free the memory for the current array delete [ ] m_theData; // now make a deep copy of the rhs m_size = rhs.m_size; m_theData = new int [ m_size ]; for (int j = 0; j < m_size; j++ ) m_theData[ j ] = rhs.m_theData [ j ]; } CMSC 202, Version 3/02

We’re Not Done Yet Recall that it’s desirable for our objects to emulate the built-in types. In particular, we can do the following with built-in types: int bob, mary, sally; bob = mary = sally; // statement 1 bob = bob; // statement 2 (bob = mary) = sally; // statement 3 So our objects should also support these statements. CMSC 202, Version 3/02

Analysis of These Statements Statement 1 is a common thing to do – it’s called cascading assignment. To accomplish this, operator= must return a reference to a SmartArray. Statement 2 is meaningless, but allowable by the language. To support this without causing a problem, operator= must check for this case (this is called self-assignment). Statement 3 is odd, but valid. To support this, operator= must return a non-const reference to a SmartArray. (Debatable -- your text does not do this.) CMSC 202, Version 3/02

SmartArray& // non-const reference SmartArray::operator= (const SmartArray& rhs) { if (this != &rhs) // not bob = bob // free the memory for the current array delete [ ] m_theData; // make a copy of the rhs m_size = rhs.m_size; m_theData = new int [m _size ]; for (int j = 0; j < m_size; j++ ) m_theData[ j ] = rhs.m_theData [ j ]; } return (*this); // for cascading assignment } CMSC 202, Version 3/02

Exercises For the Student 1) For the following statements, determine if the method called is a “regular” constructor, copy constructor, or operator= . a. SmartArray a1; b. SmartArray a2( a1 ); c. SmartArray a3 = a2; d. SmartArray a4(100 ); e. a1 = a4; CMSC 202, Version 3/02

Exercises For the Student (con’t) 2. Suppose operator= for SmartArray did not contain the statement if (this != &rhs); i.e., we allowed self-assignment. Draw the picture of memory that results from the following statements: SmartArray a1( 3 ); a1 = a1; CMSC 202, Version 3/02