The Assignment Operator

Slides:



Advertisements
Similar presentations
Introduction to Programming Lecture 39. Copy Constructor.
Advertisements

Object Oriented Programming COP3330 / CGS5409.  C++ Automatics ◦ Copy constructor () ◦ Assignment operator =  Shallow copy vs. Deep copy  DMA Review.
CSE 332: C++ copy control I Copy Control (Part I) Copy control consists of 5 distinct operations –A copy constructor initializes an object by duplicating.
Dynamically Allocated Arrays May 2, Quiz 5 Today.
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.
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.
Copy Constructors Fall 2008 Dr. David A. Gaitros
The Big Three Based on Weiss “Data Structures and algorithm Analysis CS240 Computer Science II.
Object Oriented Programming Elhanan Borenstein Lecture #3 copyrights © Elhanan Borenstein.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
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:
The Assignment Operator. Rule of Three Any object which manages memory needs: – Custom Destructor – Custom Copy Constructor – Custom Assignment Operator.
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.
Yan Shi CS/SE 2630 Lecture Notes
Constructors and Destructors
Pointers and Dynamic Arrays
Exceptions David Rabinowitz.
Linked Lists Chapter 6 Section 6.4 – 6.6
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
Andy Wang Object Oriented Programming in C++ COP 3330
Programming with ANSI C ++
Constructor & Destructor
Class Operations Pointer and References with class types
Class: Special Topics Copy Constructors Static members Friends this
Memberwise Assignment / Initialization
This pointer, Dynamic memory allocation, Constructors and Destructor
Dynamically Allocated Memory
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Array Lists Chapter 6 Section 6.1 to 6.3
Automatics, Copy Constructor, and Assignment Operator
Classes with Dynamically Allocated Data
Automatics, Copy Constructor, and Assignment Operator
Foundational Data Structures
Copy Constructor CSCE 121 J. Michael Moore.
Constructors and destructors
Constructors and Destructors
Copy Assignment CSCE 121 J. Michael Moore.
Operator overloading Dr. Bhargavi Goswami
Destruction and Copying
Indirection.
Chapter 15-3 Pointers, Dynamic Data, and Reference Types
Essential Class Operations
9-10 Classes: A Deeper Look.
C++ Constructor Insanity CSE 333 Summer 2018
Resource Allocation and Ownership
Passing Arguments and The Big 5
CSC212 Data Structure - Section RS
Review Chapter 10 PPT for full coverage.
Destruction and Copying
COP 3330 Object-oriented Programming in C++
A Deeper Look at Classes
Class: Special Topics 2 For classes using memory allocation
The Constructors Lecture 7 Fri, Feb 2, 2007.
Essential Class Operations
Copy Assignment CSCE 121.
Copy Constructor CSCE 121.
Destructors, Copy Constructors & Copy Assignment Operators
Rule of Three Part 1 & 2.
Rule of Five.
Destructors, Copy Constructors & Copy Assignment Operators
CS 144 Advanced C++ Programming April 30 Class Meeting
9-10 Classes: A Deeper Look.
CS 144 Advanced C++ Programming April 30 Class Meeting
Rule Of Three Part 3.
Copy Constructors and Overloaded Assignment
SPL – PS3 C++ Classes.
SPL – PS4 C++ Advanced OOP.
Introduction to Classes and Objects
Presentation transcript:

The Assignment Operator

Rule of Three Any object which manages memory needs: Custom Destructor Custom Copy Constructor Custom Assignment Operator

Copy Constructor Generally need a deep copy not a shallow one: YES NO

Copy Constructor Recipee Basic copy constructor recipe:

Using Copy Ctor Explicitly Explicit copy constructor call:

Using Copy Ctor Implicitly Initialization while declaring uses copy constructor:

Using Assignment Operator Assign existing object uses assignment operator

Default Assignment Operator Every class gets default assignment operator Copies each member directly: Shallow copy!!!

Custom Assignment Operator Custom assignment operator similar to copy constructor Copy over basic information Allocate own dynamic memory Copy values that are in dynamic memory

Custom Assignment Operator Custom assignment operator similar to copy constructor Copy over basic information Allocate own dynamic memory Copy values that are in dynamic memory Differences: Usually returns a reference to the object Already have an object… which already owns memory

Custom Assignment Operator Course assignment operator: I will return a reference to the course that we are copying into I take a reference to a course we will not change Interpreted as course2.=(course1)

Why return reference Chained assignments are allowed in C++ Only makes sense if y = 2 resolves to y when done

Why return reference Chained assignments are allowed in C++ y Only makes sense if y = 2 resolves to y when done y

Assignment First Pass… On assignment: Two existing objects Want to turn one into deep copy of other course2 = course1 course2.=(other) course2 is this course1 is other

Flawed first attempt… Copy basic stuff

Flawed first attempt… Delete old storage

Flawed first attempt… Allocate new storage

Flawed first attempt… Copy data

Flawed first attempt… Return the object that was copied into

Self Assignment Danger Flaw : self assignment deletes data

Bad Self Assignment Copying basic info does nothing

Bad Self Assignment Deleting wipes out storage!!!

Bad Self Assignment Allocate a new empty array

Bad Self Assignment Loop either copies empty data or blows up array up

Final Version Avoid modifying on self assignment: If address of other is same as this (my address), don't copy

Recipe Basic recipe: Step Pseudocode Verify not self assigning Copy basics Delete old memory Make new array Copy array No matter what, return self if( this != &other) { this.size = other.size … delete [] array array = new whatever[other.size] for(i = 0 to other.size -1) array[i] = other.array[i] } return *this

Rule of 3 If you manage memory you need: Custom Destructor Custom Copy Constructor Custom Assignment Operator

Rule of 5 If you manage memory you need: You may want to provide: Custom Destructor Custom Copy Constructor Custom Assignment Operator You may want to provide: Move assignment operator Move copy constructor

Move semantics C++ allows us to specify that data should be moved from one place to another:

Moving Copy:

Moving Copy:

Moving Copy:

Moving Move:

Moving Move:

Moving Move:

Defining && = Rvalue reference Move constructor/assignment take rvalue reference: Move constructor:

Defining && = Rvalue reference Move constructor/assignment take rvalue reference: Move assignment: