Top Banner
Object-Oriented Programming in C++ Pre-Lecture 6: Copy and move Prof Niels Walet ([email protected]) Room 7.07, Schuster Building February 26, 2015
37

Object-Oriented Programming in C++mccsnrw/cplusplus/PreLecture6.pdf · Object-Oriented Programming in C++ Pre-Lecture 6: Copy and move Prof Niels Walet ([email protected])

Nov 04, 2019

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Object-Oriented Programming in C++mccsnrw/cplusplus/PreLecture6.pdf · Object-Oriented Programming in C++ Pre-Lecture 6: Copy and move Prof Niels Walet (Niels.Walet@manchester.ac.uk)

Object-Oriented Programming in C++Pre-Lecture 6: Copy and move

Prof Niels Walet ([email protected])

Room 7.07, Schuster Building

February 26, 2015

Page 2: Object-Oriented Programming in C++mccsnrw/cplusplus/PreLecture6.pdf · Object-Oriented Programming in C++ Pre-Lecture 6: Copy and move Prof Niels Walet (Niels.Walet@manchester.ac.uk)

Prelecture 6Outline

In today’s lecture: focus on how to replicate objects, coveringI The assignment operatorI The copy constructorI Deep and shallow copyingI The this pointer

Two advanced aspects:I lvalues and rvaluesI move semantics

Page 3: Object-Oriented Programming in C++mccsnrw/cplusplus/PreLecture6.pdf · Object-Oriented Programming in C++ Pre-Lecture 6: Copy and move Prof Niels Walet (Niels.Walet@manchester.ac.uk)

�A basic example

Page 4: Object-Oriented Programming in C++mccsnrw/cplusplus/PreLecture6.pdf · Object-Oriented Programming in C++ Pre-Lecture 6: Copy and move Prof Niels Walet (Niels.Walet@manchester.ac.uk)

Class exampleThis week’s example: a simple class for dynamic (1D) arrays

1 #include<iostream >2 #include <stdlib.h>3 using namespace std;4 //dynamic array5 class dynarr6 {7 private:8 int size;9 double *array;10 public:11 dynarr(){cout<<"Default constructor called"<<endl; size=0; array=0;}12 dynarr(int s);13 ~dynarr(){delete array; cout<<"Destructor called"<<endl;}14 int length() const {return size;}15 double & operator[](int i);16 };17 // Parameterized constructor implementation18 dynarr::dynarr(int s)19 {20 cout<<"Parameterized constructor called"<<endl;21 if(s<1)22 {23 cout<<"Error: trying to declare an array with size < 1"<<endl;24 exit(1);25 }26 size = s;27 array = new double[size];28 for(int i=0; i<size; i++) array[i] = 0;29 }

Page 5: Object-Oriented Programming in C++mccsnrw/cplusplus/PreLecture6.pdf · Object-Oriented Programming in C++ Pre-Lecture 6: Copy and move Prof Niels Walet (Niels.Walet@manchester.ac.uk)

Class exampleThis week’s example: a simple class for dynamic (1D) arrays ct’d

31 // Overloaded element [] operator implementation32 double & dynarr::operator[](int i)33 {34 if(i<0 || i>=size)35 {36 cout<<"Error: trying to access array element out of bounds"<<endl;37 exit(1);38 }39 return array[i];40 }41 int main()42 {43 cout<<"Declaring array a1 with parameterized constructor"<<endl;44 dynarr a1(2);45 cout<<"Length of a1 = "<<a1.length()<<endl;46 a1[0] = 0.5;47 a1[1] = 1.0;48 cout<<"a1[0] = "<<a1[0]<<endl;49 cout<<"a1[1] = "<<a1[1]<<endl;50 cout<<endl;51

52 return 0;53 }

Listing 1 : selection of PL6/dynarr.cpp

Page 6: Object-Oriented Programming in C++mccsnrw/cplusplus/PreLecture6.pdf · Object-Oriented Programming in C++ Pre-Lecture 6: Copy and move Prof Niels Walet (Niels.Walet@manchester.ac.uk)

Class exampleDiversion: operator[]

We have overloaded the subscript operator double & operator[](int i); which allowed usto write

cout<<"a1[0] = "<<a1[0]<<endl;

I In line with standard C arrays - use square brackets when referring to an individual element(subscripting).

I Can also overload operator()- has advantage that it generalises to more than oneparameter, e.g. multi-dimensional arrays, my3dArray(0,0,0).

I Note that we use return by reference so that we can write a1[0] = 0.5;: The LHS returnsa reference to the value of the first element in a1 which can then be set to a different value!

Page 7: Object-Oriented Programming in C++mccsnrw/cplusplus/PreLecture6.pdf · Object-Oriented Programming in C++ Pre-Lecture 6: Copy and move Prof Niels Walet (Niels.Walet@manchester.ac.uk)

�Replication: assignment

Page 8: Object-Oriented Programming in C++mccsnrw/cplusplus/PreLecture6.pdf · Object-Oriented Programming in C++ Pre-Lecture 6: Copy and move Prof Niels Walet (Niels.Walet@manchester.ac.uk)

Replicating objects:Now let’s declare a 2nd object a2 . We can then copy its values from a1 by assignment

41 int main()42 {43 cout<<"Declaring array a1 with parameterized constructor"<<endl;44 dynarr a1(2);45 cout<<"Length of a1 = "<<a1.length()<<endl;46 a1[0] = 0.5;47 a1[1] = 1.0;48 cout<<"a1[0] = "<<a1[0]<<endl;49 cout<<"a1[1] = "<<a1[1]<<endl;50 cout<<endl;51

52 cout<<"Declaring array a2 with default constructor"<<endl;53 dynarr a2;54 cout<<"Length of a2 = "<<a2.length()<<endl;55 cout<<"Now copy values from a1 by assignment"<<endl;56 a2=a1;57 cout<<"Length of a2 = "<<a2.length()<<endl;58 cout<<"a2[0] = "<<a2[0]<<endl;59 cout<<"a2[1] = "<<a2[1]<<endl;60 cout<<endl;61

62 return 0;63 }

Listing 2 : selection of PL6/assignment.cpp

Page 9: Object-Oriented Programming in C++mccsnrw/cplusplus/PreLecture6.pdf · Object-Oriented Programming in C++ Pre-Lecture 6: Copy and move Prof Niels Walet (Niels.Walet@manchester.ac.uk)

Replicating objects:

The code now outputs

Declaring array a1 with parameterized constructorParameterized constructor calledLength of a1 = 2a1[0] = 0.5a1[1] = 1

Declaring array a2 with default constructorDefault constructor calledLength of a2 = 0Now copy values from a1 by assignmentLength of a2 = 2a2[0] = 0.5a2[1] = 1

Destructor calledDestructor called

Page 10: Object-Oriented Programming in C++mccsnrw/cplusplus/PreLecture6.pdf · Object-Oriented Programming in C++ Pre-Lecture 6: Copy and move Prof Niels Walet (Niels.Walet@manchester.ac.uk)

Replicating objects:assignment operator

I The statement a2=a1 copies the member data of a1 to a2 so they both now have the samelength and values.

I Since a2 is already instantiated, this is known as an assignment operation.I Handled by the assignment operator =.I The compiler creates a default function (operator= ) that overloads this operator for any

class.I We will see later why we usually want to do this ourselves.

Page 11: Object-Oriented Programming in C++mccsnrw/cplusplus/PreLecture6.pdf · Object-Oriented Programming in C++ Pre-Lecture 6: Copy and move Prof Niels Walet (Niels.Walet@manchester.ac.uk)

�Replication: shallow copy

Page 12: Object-Oriented Programming in C++mccsnrw/cplusplus/PreLecture6.pdf · Object-Oriented Programming in C++ Pre-Lecture 6: Copy and move Prof Niels Walet (Niels.Walet@manchester.ac.uk)

Replicating objects:I We can also copy the values while creating new objects (using initialisation)I Remember, there are two ways to do this (as with simple data types like int and double)

62 cout<<"Declare array a3 and initialize"←↩<<endl;

63 dynarr a3=a1;64 cout<<"Length of a3 = "<<a3.length()<<←↩

endl;65 cout<<"a3[0] = "<<a3[0]<<endl;66 cout<<"a3[1] = "<<a3[1]<<endl;67 cout<<endl;68

69 cout<<"Using other C++ way to declare ←↩and initialize"<<endl;

70 dynarr a4(a1);71 cout<<"Length of a4 = "<<a4.length()<<←↩

endl;72 cout<<"a4[0] = "<<a4[0]<<endl;73 cout<<"a4[1] = "<<a4[1]<<endl;74 cout<<endl;

Listing 3 : selection of PL6/initialise.cpp

Declaring array a1 with parameterized constructorParameterized constructor calledLength of a1 = 2a1[0] = 0.5a1[1] = 1

Declaring array a2 with default constructorDefault constructor calledLength of a2 = 0Now copy values from a1 by assignmentLength of a2 = 2a2[0] = 0.5a2[1] = 1

Declare array a3 and initializeLength of a3 = 2a3[0] = 0.5a3[1] = 1

Using other C++ way to declare and initializeLength of a4 = 2a4[0] = 0.5a4[1] = 1

Destructor calledDestructor calledDestructor calledDestructor called

Page 13: Object-Oriented Programming in C++mccsnrw/cplusplus/PreLecture6.pdf · Object-Oriented Programming in C++ Pre-Lecture 6: Copy and move Prof Niels Walet (Niels.Walet@manchester.ac.uk)

Replicating objects:copy constructor

I The result may seem surprising: the objects a3 and a4 did not need one of ourconstructors!

I Instead they invoked the default copy constructor.I This performs a bitwise (or like-for-like) copy of the data from one object to another.I Also known as a shallow copy (since it copies addresses, rather than the data being

pointed to!).I There are three main situations when the copy constructor is used:

I When declaring a new object as a copy of an old object (as above).I When passing an object to a function by value (need to make local copy of object in function).I When creating a temporary object (e.g. in a return statement when returning by value).

Page 14: Object-Oriented Programming in C++mccsnrw/cplusplus/PreLecture6.pdf · Object-Oriented Programming in C++ Pre-Lecture 6: Copy and move Prof Niels Walet (Niels.Walet@manchester.ac.uk)

Replicating objects:problems with shallow copying

I The above examples (using the default assignment operator and copy constructor) are fineif the objects are simple and we want to create like-for-like copies by value.

I What happens if we do the following?

76 a1[1] = -2.5;77 cout<<"a1[1] = "<<a1[1]<<endl;78 cout<<"a2[1] = "<<a2[1]<<endl;79 cout<<"a3[1] = "<<a3[1]<<endl;80 cout<<"a4[1] = "<<a4[1]<<endl;81 return 0;

Page 15: Object-Oriented Programming in C++mccsnrw/cplusplus/PreLecture6.pdf · Object-Oriented Programming in C++ Pre-Lecture 6: Copy and move Prof Niels Walet (Niels.Walet@manchester.ac.uk)

Replicating objects:problems with shallow copying

I You may (or may not!) be surprised that all 4 (a1[1], a2[1], a3[1], and a4[1]) equal -2.5!I Thus all 4 objects are modified...I When an object’s member data contains a pointer, the address is copied by the default

assignment operator and copy constructor, not the data it points to.I That is why it is called a shallow copy.I So all shallowly copied objects contain a pointer to the same data.I This can cause serious problems!

Page 16: Object-Oriented Programming in C++mccsnrw/cplusplus/PreLecture6.pdf · Object-Oriented Programming in C++ Pre-Lecture 6: Copy and move Prof Niels Walet (Niels.Walet@manchester.ac.uk)

Replicating objects:problems with shallow copying

I Currently our constructor assigns memory that never gets deleted!I It makes sense to delete array in destructor.I If we modify the code as follows

~dynarr(){cout<<"Destructor called"<<endl; delete[] array;}

I we get lots in runtime errors!I a4 is destroyed first - the destructor is called and the array is deleted.I When the destructor for a3 is called, there is no array left to delete!I Rule: for all but the simplest classes (no pointers/dynamic memory), it is much better to

write own functions to overload assignment operator and copy constructor.I We can control how dynamic arrays are copied - either just the pointer or copy the whole

array?I Latter style is known as deep copying.

Page 17: Object-Oriented Programming in C++mccsnrw/cplusplus/PreLecture6.pdf · Object-Oriented Programming in C++ Pre-Lecture 6: Copy and move Prof Niels Walet (Niels.Walet@manchester.ac.uk)

�Replication: deep copy

Page 18: Object-Oriented Programming in C++mccsnrw/cplusplus/PreLecture6.pdf · Object-Oriented Programming in C++ Pre-Lecture 6: Copy and move Prof Niels Walet (Niels.Walet@manchester.ac.uk)

Replicating objects:writing our own rules

I Copy constructor: similar to ordinary constructor but with class type as sole parameter

// Copy constructor for deep copyingdynarr::dynarr(dynarr &arr){// Copy size and declare new arrayarray=0; size=arr.length();if(size>0){array=new double[size];// Copy values into new arrayfor(int i=0;i<size;i++) array[i] = arr[i];

}}

I Used when a new object is declared as a copy of an existing objectdynarr a3=a1; dynarr a4(a1);.

Page 19: Object-Oriented Programming in C++mccsnrw/cplusplus/PreLecture6.pdf · Object-Oriented Programming in C++ Pre-Lecture 6: Copy and move Prof Niels Walet (Niels.Walet@manchester.ac.uk)

Replicating objects:writing our own rules

I Assignment operator - similar to copy constructor except that we assume the object isalready constructed!

I We must therefore delete existing data first before copying

// Assignment operator for deep copyingdynarr & dynarr::operator=(dynarr &arr){if(&arr == this) return *this; // no self assignment// First delete this object’s arraydelete[] array; array=0; size=0;// Now copy size and declare new arraysize=arr.length();if(size>0){array=new double[size];// Copy values into new arrayfor(int i=0;i<size;i++) array[i] = arr[i];

}return *this; // Special pointer!!!

}

I Used when an existing object is assigned to another dynarr a2; a2=a1;.

Page 20: Object-Oriented Programming in C++mccsnrw/cplusplus/PreLecture6.pdf · Object-Oriented Programming in C++ Pre-Lecture 6: Copy and move Prof Niels Walet (Niels.Walet@manchester.ac.uk)

Replicating objects:writing our own rules

I This is now used in the code PL6/deep.cpp; the statements

a1[1] = -2.5;cout<<"a1[1] = "<<a1[1]<<endl;cout<<"a2[1] = "<<a2[1]<<endl;cout<<"a3[1] = "<<a3[1]<<endl;cout<<"a4[1] = "<<a4[1]<<endl;return 0;

now give the output

a1[1] = -2.5a2[1] = 1a3[1] = 1a4[1] = 1

I Each object now has its own memory and copies of the data (as we performed deepcopies)

Page 21: Object-Oriented Programming in C++mccsnrw/cplusplus/PreLecture6.pdf · Object-Oriented Programming in C++ Pre-Lecture 6: Copy and move Prof Niels Walet (Niels.Walet@manchester.ac.uk)

Replicating objects:this pointer this

I The assignment operator returns a reference to the basic type, dynarr&I This is usually true for operators, so one can do things like

a=b=c; // same as a=(b=c) so b=c must have same type as a

I For operation b=c the object returned is the identical to the object calling the function(contrast this with the operation b+c where b calls function and b+c is returned)

I For this, all member functions have access to a special pointer called this which points toobject itself!

I Example: to check, we can access member data in a different wayint length() const return this->size;.

I Better use: return *this when we just want to return back the object calling the function.

Page 22: Object-Oriented Programming in C++mccsnrw/cplusplus/PreLecture6.pdf · Object-Oriented Programming in C++ Pre-Lecture 6: Copy and move Prof Niels Walet (Niels.Walet@manchester.ac.uk)

Replicating objects:this pointer this

I Another example of using the this pointer:I When we are overloading operator=, we need to protect against possible

self-assignment. Trivial dangerous example in our code: a2=a2I In this case, our code would crash since it could delete the object’s data (and allocated

memory) before trying to copy itself!I Simplest way to avoid that was to write

// Assignment operator for deep copyingdynarr & dynarr::operator=(dynarr &arr){if(&arr == this) return *this; // no self assignment

I Here, the code simply compares the address of the object (this) and the address of theargument arr to check if they are the same.

Page 23: Object-Oriented Programming in C++mccsnrw/cplusplus/PreLecture6.pdf · Object-Oriented Programming in C++ Pre-Lecture 6: Copy and move Prof Niels Walet (Niels.Walet@manchester.ac.uk)

Replicating objects:a word of caution

I When defining a function to overload the assignment operator, we returned the object byreference

myclass & myclass::operator=(myobject){... return *this;}

I Why? When returning an object by value a copy is made and returned; the original objectis then destroyed!

I Shallow-copied objects may then point to data that no longer exists.I Returning by reference avoids this when a (deep) copy constructor is not defined.I Even when it is, returning by reference is faster (but only works for objects that do not go

out of scope).

Page 24: Object-Oriented Programming in C++mccsnrw/cplusplus/PreLecture6.pdf · Object-Oriented Programming in C++ Pre-Lecture 6: Copy and move Prof Niels Walet (Niels.Walet@manchester.ac.uk)

�Summary, part 1

Page 25: Object-Oriented Programming in C++mccsnrw/cplusplus/PreLecture6.pdf · Object-Oriented Programming in C++ Pre-Lecture 6: Copy and move Prof Niels Walet (Niels.Walet@manchester.ac.uk)

Prelecture 6Summary, part 1

In today’s lecture: we looked at how and when to replicate objects, coveringI The assignment operatorI The copy constructorI Deep and shallow copyingI The this pointer

Page 26: Object-Oriented Programming in C++mccsnrw/cplusplus/PreLecture6.pdf · Object-Oriented Programming in C++ Pre-Lecture 6: Copy and move Prof Niels Walet (Niels.Walet@manchester.ac.uk)

�Part2: advanced aspects

Page 27: Object-Oriented Programming in C++mccsnrw/cplusplus/PreLecture6.pdf · Object-Oriented Programming in C++ Pre-Lecture 6: Copy and move Prof Niels Walet (Niels.Walet@manchester.ac.uk)

�Lvalues and Rvalues

Page 28: Object-Oriented Programming in C++mccsnrw/cplusplus/PreLecture6.pdf · Object-Oriented Programming in C++ Pre-Lecture 6: Copy and move Prof Niels Walet (Niels.Walet@manchester.ac.uk)

Lvalues and RvaluesNormal and temporary variables

I In the C++ specification a lot of time is spent on discussing rvalues and lvalues;I An lvalue is originally a variable that can appear on the left-hand side of an expression, and

an rvalue one that can only occur on the right-hand sideI More specifically, and lvalue is something where we can take the address, something in

(semi)permanent memory. They don’t have to be variables, e.g., a[i]=10 or morecomplicated functions are allowed (as long as we have a referable object at the left).

I An rvalue, on the other hand refers to a temporary object; in order to capture these inpermanent memory, the only way to do that is to copy them into an lvalue.

I That can be quite inefficient!

Page 29: Object-Oriented Programming in C++mccsnrw/cplusplus/PreLecture6.pdf · Object-Oriented Programming in C++ Pre-Lecture 6: Copy and move Prof Niels Walet (Niels.Walet@manchester.ac.uk)

Lvalues and Rvalueslvalue and rvalue references

I Like we said we know the address of an lvalue, so we can write lvalue&; C++11introduces the idea of an rvalue reference as well, rvalue&& (note the double ampersand!)

I Why? What is this useful for? It allows us to write functions that specifically deal with“mutable” temporary variables. Consider the following two statements

printReference (const String& str) {cout << str; }printReference (String&& str) {cout << str; }

The first one accept any constant lvalue–it actually accepts any argument it is given, lvalueor rvalue. The second overload actually picks up a mutable rvalue (no const, &&), so thegeneral function is left with the remainder,

I So we now have a way to differentiate a mutable rvalue (temporary) from all the other formsof a variable, and we can act on that. But why is that useful? The answer is ...

Page 30: Object-Oriented Programming in C++mccsnrw/cplusplus/PreLecture6.pdf · Object-Oriented Programming in C++ Pre-Lecture 6: Copy and move Prof Niels Walet (Niels.Walet@manchester.ac.uk)

�Move semantics

Page 31: Object-Oriented Programming in C++mccsnrw/cplusplus/PreLecture6.pdf · Object-Oriented Programming in C++ Pre-Lecture 6: Copy and move Prof Niels Walet (Niels.Walet@manchester.ac.uk)

Move semanticsMove constructor and move assignment

I The most common way of using this is the “move constructor” and “move assignment”. Inmany senses these parallel the copy constructors discussed before.

I The are specified almost exactly the same way, but they take an rvalue referenceI Their implementation is very differentI See the next slide for an example

Page 32: Object-Oriented Programming in C++mccsnrw/cplusplus/PreLecture6.pdf · Object-Oriented Programming in C++ Pre-Lecture 6: Copy and move Prof Niels Walet (Niels.Walet@manchester.ac.uk)

move vs copy: constructor

21 // Copy constructor for deep copying22 dynarr::dynarr(dynarr &arr)23 {24 // Copy size and declare new array25 cout <<"copy constructor\n";26 array=0; size=arr.length();27 if(size>0)28 {29 array=new double[size];30 // Copy values into new array31 for(int i=0;i<size;i++) array[i] = arr[i];32 }33 }

34 // Move constructor35 dynarr::dynarr(dynarr &&arr)36 { // steal the data37 cout <<"move constructor\n";38 size=arr.size;39 array=arr.array;40 arr.size=0;41 arr.array=0;//nullptr;42 }

Listing 4 : selection of PL6/move.cpp

Page 33: Object-Oriented Programming in C++mccsnrw/cplusplus/PreLecture6.pdf · Object-Oriented Programming in C++ Pre-Lecture 6: Copy and move Prof Niels Walet (Niels.Walet@manchester.ac.uk)

move vs copy: assignment

43 // Assignment operator for deep copying44 dynarr & dynarr::operator=(dynarr &arr)45 {46 cout <<"copy assignment\n";47 if(&arr == this) return *this; // no self assignment48 // First delete this object’s array49 delete[] array; array=0; size=0;50 // Now copy size and declare new array51 size=arr.length();52 if(size>0)53 {54 array=new double[size];55 // Copy values into new array56 for(int i=0;i<size;i++) array[i] = arr[i];57 }58 return *this; // Special pointer!!!59 }

60 // Move assignment operator61 dynarr & dynarr::operator=(dynarr&& arr)62 {63 cout <<"move assignment\n";64 std::swap(size,arr.size);65 std::swap(array,arr.array);66 return *this; // Special pointer!!!67 }

Listing 5 : selection of PL6/move.cpp

Page 34: Object-Oriented Programming in C++mccsnrw/cplusplus/PreLecture6.pdf · Object-Oriented Programming in C++ Pre-Lecture 6: Copy and move Prof Niels Walet (Niels.Walet@manchester.ac.uk)

Move semanticsstd::move

I Suppose I know an lvalue object is no longer useful, and I do want to use the moveassignment to reassign its data

I Is there a way to do this?I Need to turn (cast?) an lvalue to an rvalueI Can be done by using a static_cast using an rvalue reference, but nicer is thestd::move function defined in C++

I Misnamed, because it turns an lvalue into something that can be used like an rvalue (andthus its data can be moved, and the objects content destroyed).

Page 35: Object-Oriented Programming in C++mccsnrw/cplusplus/PreLecture6.pdf · Object-Oriented Programming in C++ Pre-Lecture 6: Copy and move Prof Niels Walet (Niels.Walet@manchester.ac.uk)

move vs copy: assignment

113 cout<<"Declaring array a3 with parameterized constructor"←↩

<<endl;114 dynarr a3(2);115 cout<<"Length of a3 = "<<a3.length()<<endl;116 a3[0] = 0.5;117 a3[1] = 1.0;118 cout<<"a3[0] = "<<a3[0]<<endl;119 cout<<"a3[1] = "<<a3[1]<<endl;120 cout<<endl;121 cout<<"Now move values from a1 by assignment"<<endl;122 dynarr a4;123 a4= move(a3);124 cout<<"Length of a4 = "<<a4.length()<<" and of a3 ="<<a3.←↩

length()<<endl;125 cout<<"a4[0] = "<<a4[0]<<endl;126 cout<<"a4[1] = "<<a4[1]<<endl;127 cout<<endl;

Listing 6 : selection of PL6/move.cpp

Declaring array a3 with parameterized constructorParameterized constructor calledLength of a3 = 2a3[0] = 0.5a3[1] = 1

Now move values from a1 by assignmentDefault constructor calledmove assignmentLength of a4 = 2 and of a3 =0a4[0] = 0.5a4[1] = 1

Listing 7 : selection of PL6/move.out

Page 36: Object-Oriented Programming in C++mccsnrw/cplusplus/PreLecture6.pdf · Object-Oriented Programming in C++ Pre-Lecture 6: Copy and move Prof Niels Walet (Niels.Walet@manchester.ac.uk)

�Summary, part 2

Page 37: Object-Oriented Programming in C++mccsnrw/cplusplus/PreLecture6.pdf · Object-Oriented Programming in C++ Pre-Lecture 6: Copy and move Prof Niels Walet (Niels.Walet@manchester.ac.uk)

Prelecture 6Summary, part 2

In the second part of today’s lecture: we looked at how and when to replicate objects, coveringI lvalues, rvaluesI move semantics