Destructors, Copy Constructors & Copy Assignment Operators

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

Introduction to Programming Lecture 39. Copy Constructor.
Introduction to Java Objects CSIS 3701: Advanced Object Oriented Programming.
Winter 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assn 4 posted shortly. Demonstrate a memory leak problem using the assignment 4 solution. Back to.
Classes Separating interface from implementation
Constructors & Destructors Review CS 308 – Data Structures.
Review of C++ Programming Part II Sheng-Fang Huang.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
Copy Control Joe Meehean. More Class Responsibilities When making a new type (i.e., class) we must specify what happens when it is: Copied Assigned Destroyed.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
CS 11 C++ track: lecture 4 Today: More on memory management the stack and the heap inline functions structs vs. classes.
1 Overloading Overloading allows a function or operator to have a different meaning depending on the type of objects it is used on. Examples: operator+
1 CSC241: Object Oriented Programming Lecture No 22.
Memory Management Issues, Solutions, and Examples.
Dynamically Allocated Arrays December 4, Skip the Rest of this PowerPoint.
More C++ Features True object initialisation
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
CMSC 202, Version 3/02 1 Copy Constructors and Overloaded Assignment.
Object Management. Constructors –Compiler-generated –The Initializer List –Copy Constructors –Single-arg (conversion ctors) The Assignment Operator.
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.
1 Review: C++ class represents an ADT 2 kinds of class members: data members and function members Class members are private by default Data members are.
C String, Proper Type, and String Objects Andy Wang COP 4530: Data Structures, Algorithms, and Generic Programming.
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 Memory as byte array Pointers Arrays relationship to pointers Operator ‘new’ Operator ‘delete’ Copy ctor Assignment operator ‘this’ const pointer Allocating.
Previous lecture Introduction to OOP and C++ Data Abstraction –String example.
Classes II Lecture 7 Course Name: High Level Programming Language Year : 2010.
1 Introduction to Object Oriented Programming Chapter 10.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
1 Ugly Realities The Dark Side of C++ Chapter 12.
Memory Management.
Yan Shi CS/SE 2630 Lecture Notes
Pointers and Dynamic Arrays
CSC241: Object Oriented Programming
Concepts of Constructors and Its Types
Class: Special Topics Copy Constructors Static members Friends this
Pointers Revisited What is variable address, name, value?
LinkedList Class.
Memberwise Assignment / Initialization
This pointer, Dynamic memory allocation, Constructors and Destructor
Dynamically Allocated Memory
Dynamic Memory Allocation
CSC 253 Lecture 8.
Chapter 16-2 Linked Structures
CSC 253 Lecture 8.
Basic C++ What’s a declaration? What’s a definition?
Chapter 15 Pointers, Dynamic Data, and Reference Types
Automatics, Copy Constructor, and Assignment Operator
CS212: Object Oriented Analysis and Design
Constant pointers and pointers to constants
Dynamic Memory Allocation
Chapter 15 Pointers, Dynamic Data, and Reference Types
Indirection.
Chapter 16 Linked Structures
Dynamic Memory Management
Essential Class Operations
CS148 Introduction to Programming II
9-10 Classes: A Deeper Look.
Pointer Variables A pointer is a variable that contains a memory address The address is commonly the location of another variable in memory This pointer.
COP 3330 Object-oriented Programming in C++
A Deeper Look at Classes
Review: C++ class represents an ADT
Class: Special Topics 2 For classes using memory allocation
Pointers and References
Essential Class Operations
Dynamic Memory Management
Destructors, Copy Constructors & Copy Assignment Operators
Rule of Three Part 1 & 2.
9-10 Classes: A Deeper Look.
Copy Constructors and Overloaded Assignment
SPL – PS3 C++ Classes.
Presentation transcript:

Destructors, Copy Constructors & Copy Assignment Operators Adapted from Dr. Mary Eberlein

Non-Default Destructor When do you need it? Memory is allocated in your constructor: use a destructor to delete it. Otherwise: memory leaks

Copy Constructor & Copy Assignment Operator Two ways to copy an object: copy constructor: create a new object that's a copy of an existing object copy assignment operator: set an existing object equal to another object For existing Person object p: Copy constructor: Person p2(p); // create new object p2 using state of existing object p Or equivalently: Person p2 = p; Copy assignment operator: Person p2 = ...; p2 = p; // p2 is a Person object, and we want to set its state equal to that of p If we don't define them in our classes, we get a default destructor, copy constructor and copy assignment operator. When do we need to define our own? If an object has pointers or some other run- time allocation of a resource (e.g., opening a file). default copy constructor and assignment operator will do a shallow copy default destructor will not deallocate memory allocated for an instance variable

Example class String { private: char* str; int length; public: String(const char* str = NULL); // constructor ~String(); String(const String&); // copy constructor // other member functions... };

Shallow Copy Default assignment operator and copy constructor produce a shallow copy String s1("hello"); String s2 = s1; // default copy constructor invoked s1 s2 str length str length 5 5 'h' 'e' 'l' 'o' '\0'

Default Destructor Default destructor does not free the heap-allocated array that the str instance variable points to String* s1 = new String("hello"); delete s1; // default destructor results in memory leak s1 str length 5 'h' 'e' 'l' 'o' '\0'

Example String::String(const char *s) { length = strlen(s); str = new char[length+1]; strcpy(str, s); } // copy constructor String::String(const String& oldStr){ length = oldStr.length; strcpy(str, oldStr.str); String::~String() { delete [] str; } // copy assignment operator String& String::operator =(const String &s){ if(this != &s) { delete[] str; length = s.length; str = new char[length+1]; strcpy(str, s.str); return *this; Make sure assignment operator works if we do something like this: x = x; (That's why we include the if) Return a reference to this object so that chaining of assignments works: x = y = z;

Copy Constructor & Copy Assignment To avoid implementing them, you can declare them to be private in your class. For example if you don’t want an object to be copied at all. Log files, certain design patterns, etc. Then if a client attempts to invoke them, they get a compile error. The Rule of Three is a rule of thumb for C++: if your class needs to define any of copy constructor, assignment operator, destructor, then it needs all three of them.