Rule of Five.

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.
Chapter 1 OO using C++. Abstract Data Types Before we begin we should know how to accomplish the goal of the program We should know all the input and.
CSE 332: C++ copy control II Copy Control (Part II) Review: copy control consists of 5 distinct operations –A copy constructor initializes an object by.
Linked List Improvements & Memory. BigO's What is BigO for our basic linked list operations? InsertStart Insert at middle InsertEnd Retrieve First Value.
@ Zhigang Zhu, CSC212 Data Structure - Section FG Lecture 10 The Bag and Sequence Classes with Linked Lists Instructor: Zhigang Zhu Department.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
Stacks CS-240 & CS-341 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed.
Java Objects and Classes. Overview n Creating objects that belong to the classes in the standard Java library n Creating your own classes.
Memory Management Issues, Solutions, and Examples.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
The Assignment Operator. Rule of Three Any object which manages memory needs: – Custom Destructor – Custom Copy Constructor – Custom Assignment Operator.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Destructors The destructor fulfills the opposite functionality. It is automatically called when an object.
Yan Shi CS/SE 2630 Lecture Notes
Constructors and Destructors
Learning Objectives Pointers as dada members
Memory Management with Classes
Programming with ANSI C ++
Chapter 1 C++ Basics Review
CISC181 Introduction to Computer Science Dr
Object-Oriented Programming & Design Lecture 18 Martin van Bommel
ASSIGNMENT NO.-2.
Finally! Discussing a language!

This pointer, Dynamic memory allocation, Constructors and Destructor
PRG 218 Week 5 Individual Assignment Coding: Derived Classes Please click here to buy ( (
group work #hifiTeam
Basic C++ What’s a declaration? What’s a definition?
Foundational Data Structures
The Assignment Operator
Copy Constructor CSCE 121 J. Michael Moore.
Constructors and destructors
Constructors and Destructors
Copy Assignment CSCE 121 J. Michael Moore.
Destruction and Copying
Rules of Composition Photography 1 Ms. Brown
Eyvind W. Axelsen INF3110: Solutions to Problem Set 2 Scoping, Runtime Organisation, Parameter Passing Methods Eyvind W. Axelsen.
Indirection.
Summary: Abstract Data Type
Assessment – Java Basics: Part 1
Inheritance: The Fundamental Functions
Dynamic Memory Management
Essential Class Operations
The Big 5 and Lists.
Class and Objects In a class, all the functions that operate on the data structure are grouped together in one place along with the data Like a struct.
Resource Allocation and Ownership
Passing Arguments and The Big 5
Review Chapter 10 PPT for full coverage.
Destruction and Copying
NAME 436.
2010 Audit Summary Results YEAR 100% Verified Failed Reduced 2010
Passing Arguments and The Big 5
7th Grade Feb. 4 Assignment
Move Semantics CSCE 121.
Class: Special Topics 2 For classes using memory allocation
The Constructors Lecture 7 Fri, Feb 2, 2007.
ENERGY 211 / CME 211 Lecture 30 December 5, 2008.
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Essential Class Operations
Copy Assignment CSCE 121.
Dynamic Memory Management
Copy Constructor 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
Rule Of Three Part 3.
C++ support for Object-Oriented Programming
SPL – PS3 C++ Classes.
SPL – PS4 C++ Advanced OOP.
Presentation transcript:

Rule of Five

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:

Rule of 5 Why? But… Move can be more efficient than copying Never required for program correctness, just efficiency