Special Member Functions. Compiler-Generated Destructor. Compiler-Generated Default Constructor. Special Member Functions

Similar documents
Special Member Functions

Copy Constructor, Assignment, Equality

Entity vs. Value, Modules, Hidden Implementation, Interface Specification

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

Value vs. Entity Objects, Information Hiding

THE NAME OF THE CONSTRUCTOR AND DESTRUCTOR(HAVING (~) BEFORE ITS NAME) FUNCTION MUST BE SAME AS THE NAME OF THE CLASS IN WHICH THEY ARE DECLARED.

Modern C++ for Computer Vision and Image Processing. Igor Bogoslavskyi

04-19 Discussion Notes

static CS106L Spring 2009 Handout #21 May 12, 2009 Introduction

COMP6771 Advanced C++ Programming

C++ 8. Constructors and Destructors

Object-Oriented Principles and Practice / C++

Abstract Data Types (ADTs) 1. Legal Values. Client Code for Rational ADT. ADT Design. CS 247: Software Engineering Principles

Object-Oriented Principles and Practice / C++

CS 247: Software Engineering Principles. ADT Design

Written by John Bell for CS 342, Spring 2018

More About Classes. Gaddis Ch. 14, CS 2308 :: Fall 2015 Molly O'Neil

Chapter 10. Pointers and Dynamic Arrays. Copyright 2016 Pearson, Inc. All rights reserved.

Determine a word is a palindrome? bool ispalindrome(string word, int front, int back) { Search if a number is in an array

And Even More and More C++ Fundamentals of Computer Science

Copying Data. Contents. Steven J. Zeil. November 13, Destructors 2

The issues. Programming in C++ Common storage modes. Static storage in C++ Session 8 Memory Management

A506 / C201 Computer Programming II Placement Exam Sample Questions. For each of the following, choose the most appropriate answer (2pts each).

Constants, References

Vector and Free Store (Vectors and Arrays)

KOM3191 Object Oriented Programming Dr Muharrem Mercimek OPERATOR OVERLOADING. KOM3191 Object-Oriented Programming

Assertions and Exceptions

G52CPP C++ Programming Lecture 13

Chapter 10 Pointers and Dynamic Arrays. GEDB030 Computer Programming for Engineers Fall 2017 Euiseong Seo

Assignment operator string class c++ Assignment operator string class c++.zip

Launchpad Lecture -10

6.096 Introduction to C++ January (IAP) 2009

CS32 - Week 1. Umut Oztok. June 24, Umut Oztok CS32 - Week 1

Copy Constructors and Assignment Operators

2 ADT Programming User-defined abstract data types

Make Classes Useful Again

Object-Oriented Programming

Midterm Review. PIC 10B Spring 2018

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

Everything You Ever Wanted To Know About Move Semantics

CS 247: Software Engineering Principles. C++ Templates. Reading: Eckel, Vol. 2 Ch. 5 Templates in Depth. U Waterloo CS247 (Spring 2017) p.

COMP6771 Advanced C++ Programming

Advanced Systems Programming

Introduction to Classes

Programmazione. Prof. Marco Bertini

Assignment of Structs

CS 247: Software Engineering Principles. Modules

Constructors for classes

Overloading Operators in C++

Programming, numerics and optimization

CSE 333. Lecture 11 - constructor insanity. Hal Perkins Paul G. Allen School of Computer Science & Engineering University of Washington

Object-Oriented Languages and Object-Oriented Design. Ghezzi&Jazayeri: OO Languages 1

Object Oriented Programming COP3330 / CGS5409

See the CS 2704 notes on C++ Class Basics for more details and examples. Data Structures & OO Development I

Object-Oriented Programming, Iouliia Skliarova

Object-Oriented Principles and Practice / C++

Software Engineering Concepts: Invariants Silently Written & Called Functions Simple Class Example

Intro to OOP Visibility/protection levels and constructors Friend, convert constructor, destructor Operator overloading a<=b a.

Assignment of Objects

CMSC 132: Object-Oriented Programming II

CSCE 110 PROGRAMMING FUNDAMENTALS

Class Vector: Interface CSE 143. Dynamic Memory In Classes [Chapter 4, p ] Vector Implementation. Many Ways to Implement. Draw the picture!

CS260 Intro to Java & Android 03.Java Language Basics

Exception Safety. CS 311 Data Structures and Algorithms Lecture Slides Wednesday, October 28, Glenn G. Chappell. continued

The paramaterless ctor (aka default ctor)

More class design with C++ Starting Savitch Chap. 11

Ch. 12: Operator Overloading

Advanced C++ Topics. Alexander Warg, 2017

Data types. Definitions Aggregate constructors User-defined type definitions Types and storage Types and subtypes Type systems and type checking

Dynamic memory in class Ch 9, 11.4, 13.1 & Appendix F

Ch02. True/False Indicate whether the statement is true or false.

Lab 2: ADT Design & Implementation

CS304 Object Oriented Programming Final Term

CS24 Week 3 Lecture 1

THE BIG FOUR FRIEND FUNCTIONS

CS 247 Software Engineering Principles Midterm Examination **Solutions** Spring 2017

C++ Addendum: Inheritance of Special Member Functions. Constructors Destructor Construction and Destruction Order Assignment Operator

A <Basic> C++ Course

Fast Introduction to Object Oriented Programming and C++

Overview of C Feabhas Ltd 2012

A <Basic> C++ Course

Intermediate Programming, Spring 2017*

Refresher: Interface Specifications. ADT Documentation. Set Represented as an Array. Representation Invariant, Abstraction Function

CIS 265/506 Exam1 Spring 2012 Prof. V. Matos Exam Last Name First Name:

Linked Lists. Gaddis Ch. 17. CS 2308 :: Spring 2016 Molly O'Neil

LECTURE 03 LINKED LIST

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 12: Classes and Data Abstraction

Comp151. Generic Programming: Container Classes

Representation Invariant, Abstraction Function

An Introduction to C++

PIC 10B Lecture 1 Winter 2014 Homework Assignment #3

Multiple choice questions. Answer on Scantron Form. 4 points each (100 points) Which is NOT a reasonable conclusion to this sentence:

Structs. Contiguously-allocated region of memory Refer to members within structure by names Members may be of different types Example: Memory Layout

Recharge (int, int, int); //constructor declared void disply();

CS250 Final Review Questions

Chapter 13: Copy Control. Overview. Overview. Overview

CSE 143. Linked Lists. Linked Lists. Manipulating Nodes (1) Creating Nodes. Manipulating Nodes (3) Manipulating Nodes (2) CSE 143 1

CSC1322 Object-Oriented Programming Concepts

Module Operator Overloading and Type Conversion. Table of Contents

Short Notes of CS201

Transcription:

Special Member Functions CS 247: Software Engineering Principles Special Member Functions Readings: Eckel, Vol. 1 Ch. 11 References and the Copy Constructor Ch. 12 Operator Overloading ( operator= ) C++ member functions that are so important that the compiler will provide versions if we don't provide them: (0 parameters; generated iff we define no ) ( operator= ) U Waterloo CS247 (Spring 2017) p.1/20 U Waterloo CS247 (Spring 2017) p.2/20 Compiler-Generated Default Constructor If we do declare any for our class, the compiler will generate a for us: based on memberwise initialization. simple data members (built-in types): uninitialized pointer members: uninitialized member objects: initialized using members' s inherited members: initialized using base class Compiler-Generated Destructor If we do declare a for our class, the compiler will generate a for us: based on memberwise destruction simple data members: deallocated pointer members: pointer deallocated ( deleted) member objects: cleaned up using members' s inherited members: cleaned up using base class's U Waterloo CS247 (Spring 2017) p.3/20 U Waterloo CS247 (Spring 2017) p.4/20

Copy Constructor A constructs a new object whose value is equal to an existing object. - Used by the compiler to objects of the ADT. class Money; Money operator+ (Money m, Money n); int main() { Money m; Money n{m; Money p = m; p = p + n; U Waterloo CS247 (Spring 2017) p.5/20 Shallow copies the object and its pointers' addresses, so that the original and copied pointers refer to the same object. Deep copies the object and what its pointers point to, so that the pointer data members refer to distinct objects. Copying Objects with Pointers U Waterloo CS247 (Spring 2017) p.6/20 Compiler-Generated Copy Constructor If we do provide a or / or /, the compiler will generate a for us: based on memberwise simple data members: bitwise pointer members: bitwise member objects: copied using members' s inherited members: copied using base class's Copy is similar to the, except that the destination of the already exists. = ; // deep Copy Assignment Operator U Waterloo CS247 (Spring 2017) p.7/20 U Waterloo CS247 (Spring 2017) p.8/20

Compiler-Generated Assignment Operator If we do provide a / or /, the compiler will create a operator= member function for us: based on memberwise : simple data members: bitwise pointer members: bitwise member objects: uses members' operators inherited members: uses base class's operator = ; // shallow U Waterloo CS247 (Spring 2017) p.9/20 Copy-Swap Idiom Assignment operators may deal with pointer members by: creating a new object of the same type with the swapping the old values of the pointer members with the values in the newly created object letting the take care of deleting the old members but at the cost of efficiency!! // friend that swaps contents of m1 and m2 // using std::swap from <algorithm> void swap( MyClass & m1, MyClass &m2 ) { Base &b_m1 = static_cast<base&>(m1); Base &b_m2 = static_cast<base&>(m2); swap( b_m1, b_m2 ); std::swap( m1., m2. ); std::swap( m1., m2. ); std::swap( m1., m2. ); MyClass& MyClass::operator= (const MyClass& m) { MyClass temp{m; swap( *this, temp ); U Waterloo CS247 (Spring 2017) p.10/20 A constructs a new object whose value is equal to an existing object, but does preserve the value of the existing object. MyClass::MyClass (MyClass&& m) : Base{ std::(m), { std::(m.), { m.simple, { m. { m. = nullptr; Move Constructor To invoke members, need to pass an rvalue reference created by std::() Only requirement of d-from object is that it be easy to delete and () reassign. Compiler-Generated Move Constructor If we do provide a or / or /, then the compiler will generate a for us: based on memberwise simple data members: bitwise pointer members: bitwise member objects: uses members' / s inherited members: uses base class's / U Waterloo CS247 (Spring 2017) p.11/20 U Waterloo CS247 (Spring 2017) p.12/20

Move is similar to the, except that the destination of the already exists. Only requirement of d-from object is that it be easy to delete and () reassign. MyClass& MyClass::operator= (MyClass&& m) { MyClass temp(0); swap( temp, m ); // temp steals m s contents swap( *this, temp ); // put what used to be in // m into this Move Assignment Improved Assignment We can improve the code by passing the argument by value and unifying the and operators into a single member method. Compiler will call this operator even with an rvalue. MyClass::MyClass( const MyClass & other) : MyClass(0) { ( *this, other ); MyClass::MyClass( MyClass && other ) : MyClass(0) { swap( *this, other ); MyClass& MyClass::operator= (MyClass m) { swap( *this, m ); U Waterloo CS247 (Spring 2017) p.13/20 U Waterloo CS247 (Spring 2017) p.14/20 Compiler-Generated Move Assignment If we do provide a / or /, the compiler will create a operator= member function for us: based on memberwise : simple data members: bitwise pointer members: bitwise member objects: uses members' / inherited members: uses base class's / = ; // shallow U Waterloo CS247 (Spring 2017) p.15/20 http://accu.org/content/conf2014/howard_hinnant_accu_2014.pdf Special Members declares Nothing Any compiler implicitly declares deleted deleted deleted deleted

http://accu.org/content/conf2014/howard_hinnant_accu_2014.pdf Special Members declares Nothing Any compiler implicitly declares deleted deleted deleted deleted Deep equality Shallow equality Equality A copied/assigned object should be equal ( operator== ) to the original. compares compares The compiler does NOT generate a version of the equality operator we are on our own. U Waterloo CS247 (Spring 2017) p.18/20 Equality bool operator== ( const MyClass &m, const MyClass &m2 ) { if (!( Base::operator==(m, m2) ) ) return false; Take Aways Recognition C++ special member functions (6 of them): if one needs to be hand-crafted, likely all of them do if ( m.!= m2. ) return false; if (! (m. == m2.) ) return false; if (! m.p_ &&! m2.p_ ) return true; if (! m.p_! m2.p_ ) return false; return *m.p_ == *m2.p_ ; U Waterloo CS247 (Spring 2017) p.19/20 Comprehension Best practices for ADT design Entity vs. Value-based ADTs Rules for compiler- special member functions (,,, ) Application Operator overloading Const function arguments and member functions ADT design (entity vs. value-based design, immutable ADTs, hidden implementation) User-defined s,,, U Waterloo CS247 (Spring 2017) p.20/20