Lecture 13: Special Member Functionsweb.stanford.edu/class/cs106l/lectures/L13_Special...CS 106L,...
Transcript of Lecture 13: Special Member Functionsweb.stanford.edu/class/cs106l/lectures/L13_Special...CS 106L,...
![Page 1: Lecture 13: Special Member Functionsweb.stanford.edu/class/cs106l/lectures/L13_Special...CS 106L, Fall ‘20 CS 106B covers the barebones of C++ classes we’ll be covering the rest](https://reader035.fdocuments.us/reader035/viewer/2022071407/60fe26c3501e8d22bb2d7fcd/html5/thumbnails/1.jpg)
Lecture 13: Special Member FunctionsCS 106L, Fall ‘20
![Page 2: Lecture 13: Special Member Functionsweb.stanford.edu/class/cs106l/lectures/L13_Special...CS 106L, Fall ‘20 CS 106B covers the barebones of C++ classes we’ll be covering the rest](https://reader035.fdocuments.us/reader035/viewer/2022071407/60fe26c3501e8d22bb2d7fcd/html5/thumbnails/2.jpg)
CS 106B covers the barebones of C++ classeswe’ll be covering the rest
template classes • const correctness • operator overloading special member functions • move semantics • RAII
![Page 3: Lecture 13: Special Member Functionsweb.stanford.edu/class/cs106l/lectures/L13_Special...CS 106L, Fall ‘20 CS 106B covers the barebones of C++ classes we’ll be covering the rest](https://reader035.fdocuments.us/reader035/viewer/2022071407/60fe26c3501e8d22bb2d7fcd/html5/thumbnails/3.jpg)
CS 106B covers the barebones of C++ classeswe’ll be covering the rest
template classes • const correctness • operator overloading special member functions • move semantics • RAII
![Page 4: Lecture 13: Special Member Functionsweb.stanford.edu/class/cs106l/lectures/L13_Special...CS 106L, Fall ‘20 CS 106B covers the barebones of C++ classes we’ll be covering the rest](https://reader035.fdocuments.us/reader035/viewer/2022071407/60fe26c3501e8d22bb2d7fcd/html5/thumbnails/4.jpg)
Key questions we will answer today
● What are special member functions? When are they called?● When should we declare a special member function?● When should we not declare a special member function?
![Page 5: Lecture 13: Special Member Functionsweb.stanford.edu/class/cs106l/lectures/L13_Special...CS 106L, Fall ‘20 CS 106B covers the barebones of C++ classes we’ll be covering the rest](https://reader035.fdocuments.us/reader035/viewer/2022071407/60fe26c3501e8d22bb2d7fcd/html5/thumbnails/5.jpg)
Agenda
● More info about mycollection::vector implementation● Intro to special member functions● Member initializer lists● Why aren’t the default functions always sufficient?● Copy assignment and construction● Delete● Rule of three/zero
![Page 6: Lecture 13: Special Member Functionsweb.stanford.edu/class/cs106l/lectures/L13_Special...CS 106L, Fall ‘20 CS 106B covers the barebones of C++ classes we’ll be covering the rest](https://reader035.fdocuments.us/reader035/viewer/2022071407/60fe26c3501e8d22bb2d7fcd/html5/thumbnails/6.jpg)
Live Code Demo:mycollection::vector implementation
![Page 7: Lecture 13: Special Member Functionsweb.stanford.edu/class/cs106l/lectures/L13_Special...CS 106L, Fall ‘20 CS 106B covers the barebones of C++ classes we’ll be covering the rest](https://reader035.fdocuments.us/reader035/viewer/2022071407/60fe26c3501e8d22bb2d7fcd/html5/thumbnails/7.jpg)
Intro to special member functions
![Page 8: Lecture 13: Special Member Functionsweb.stanford.edu/class/cs106l/lectures/L13_Special...CS 106L, Fall ‘20 CS 106B covers the barebones of C++ classes we’ll be covering the rest](https://reader035.fdocuments.us/reader035/viewer/2022071407/60fe26c3501e8d22bb2d7fcd/html5/thumbnails/8.jpg)
Special member functions are (usually) automatically generated by the compiler
![Page 9: Lecture 13: Special Member Functionsweb.stanford.edu/class/cs106l/lectures/L13_Special...CS 106L, Fall ‘20 CS 106B covers the barebones of C++ classes we’ll be covering the rest](https://reader035.fdocuments.us/reader035/viewer/2022071407/60fe26c3501e8d22bb2d7fcd/html5/thumbnails/9.jpg)
Special Member Functions
● Default construction: object created with no parameters.
● Copy construction: object is created as a copy of an existing object.
● Copy assignment: existing object replaced as a copy of another existing object.
● Destruction: object destroyed when it is out of scope.
![Page 10: Lecture 13: Special Member Functionsweb.stanford.edu/class/cs106l/lectures/L13_Special...CS 106L, Fall ‘20 CS 106B covers the barebones of C++ classes we’ll be covering the rest](https://reader035.fdocuments.us/reader035/viewer/2022071407/60fe26c3501e8d22bb2d7fcd/html5/thumbnails/10.jpg)
What Special Member Function is Called on Each Line?
vector<int> function(vector<int> vec0) { vector<int> vec1; vector<int> vec2(3); vector<int> vec3{3}; vector<int> vec4(); vector<int> vec5(vec2); vector<int> vec6{}; vector<int> vec7{vec3 + vec4}; vector<int> vec8 = vec4; vec8 = vec2; return vec8;}
● Default construction: object created with no parameters.
● Copy construction: object is created as a copy of an existing object.
● Copy assignment: existing object replaced as a copy of another existing object.
● Destruction: object destroyed when it is out of scope.
![Page 11: Lecture 13: Special Member Functionsweb.stanford.edu/class/cs106l/lectures/L13_Special...CS 106L, Fall ‘20 CS 106B covers the barebones of C++ classes we’ll be covering the rest](https://reader035.fdocuments.us/reader035/viewer/2022071407/60fe26c3501e8d22bb2d7fcd/html5/thumbnails/11.jpg)
Copy constructor (passing by value)
vector<int> function(vector<int> vec0) { vector<int> vec1; vector<int> vec2(3); vector<int> vec3{3}; vector<int> vec4(); vector<int> vec5(vec2); vector<int> vec6{}; vector<int> vec7{vec3 + vec4}; vector<int> vec8 = vec4; vec8 = vec2; return vec8;}
● Default construction: object created with no parameters.
● Copy construction: object is created as a copy of an existing object.
● Copy assignment: existing object replaced as a copy of another existing object.
● Destruction: object destroyed when it is out of scope.
![Page 12: Lecture 13: Special Member Functionsweb.stanford.edu/class/cs106l/lectures/L13_Special...CS 106L, Fall ‘20 CS 106B covers the barebones of C++ classes we’ll be covering the rest](https://reader035.fdocuments.us/reader035/viewer/2022071407/60fe26c3501e8d22bb2d7fcd/html5/thumbnails/12.jpg)
Default constructor creates empty vector
vector<int> function(vector<int> vec0) { vector<int> vec1; vector<int> vec2(3); vector<int> vec3{3}; vector<int> vec4(); vector<int> vec5(vec2); vector<int> vec6{}; vector<int> vec7{vec3 + vec4}; vector<int> vec8 = vec4; vec8 = vec2; return vec8;}
● Default construction: object created with no parameters.
● Copy construction: object is created as a copy of an existing object.
● Copy assignment: existing object replaced as a copy of another existing object.
● Destruction: object destroyed when it is out of scope.
![Page 13: Lecture 13: Special Member Functionsweb.stanford.edu/class/cs106l/lectures/L13_Special...CS 106L, Fall ‘20 CS 106B covers the barebones of C++ classes we’ll be covering the rest](https://reader035.fdocuments.us/reader035/viewer/2022071407/60fe26c3501e8d22bb2d7fcd/html5/thumbnails/13.jpg)
Not a special member function - creates a vector {0, 0, 0}
vector<int> function(vector<int> vec0) { vector<int> vec1; vector<int> vec2(3); vector<int> vec3{3}; vector<int> vec4(); vector<int> vec5(vec2); vector<int> vec6{}; vector<int> vec7{vec3 + vec4}; vector<int> vec8 = vec4; vec8 = vec2; return vec8;}
● Default construction: object created with no parameters.
● Copy construction: object is created as a copy of an existing object.
● Copy assignment: existing object replaced as a copy of another existing object.
● Destruction: object destroyed when it is out of scope.
![Page 14: Lecture 13: Special Member Functionsweb.stanford.edu/class/cs106l/lectures/L13_Special...CS 106L, Fall ‘20 CS 106B covers the barebones of C++ classes we’ll be covering the rest](https://reader035.fdocuments.us/reader035/viewer/2022071407/60fe26c3501e8d22bb2d7fcd/html5/thumbnails/14.jpg)
Also not a special member function, uses initializer_list
vector<int> function(vector<int> vec0) { vector<int> vec1; vector<int> vec2(3); vector<int> vec3{3}; vector<int> vec4(); vector<int> vec5(vec2); vector<int> vec6{}; vector<int> vec7{vec3 + vec4}; vector<int> vec8 = vec4; vec8 = vec2; return vec8;}
● Default construction: object created with no parameters.
● Copy construction: object is created as a copy of an existing object.
● Copy assignment: existing object replaced as a copy of another existing object.
● Destruction: object destroyed when it is out of scope.
![Page 15: Lecture 13: Special Member Functionsweb.stanford.edu/class/cs106l/lectures/L13_Special...CS 106L, Fall ‘20 CS 106B covers the barebones of C++ classes we’ll be covering the rest](https://reader035.fdocuments.us/reader035/viewer/2022071407/60fe26c3501e8d22bb2d7fcd/html5/thumbnails/15.jpg)
This is a function declaration! (C++’s Most Vexing Parse)
vector<int> function(vector<int> vec0) { vector<int> vec1; vector<int> vec2(3); vector<int> vec3{3}; vector<int> vec4(); vector<int> vec5(vec2); vector<int> vec6{}; vector<int> vec7{vec3 + vec4}; vector<int> vec8 = vec4; vec8 = vec2; return vec8;}
● Default construction: object created with no parameters.
● Copy construction: object is created as a copy of an existing object.
● Copy assignment: existing object replaced as a copy of another existing object.
● Destruction: object destroyed when it is out of scope.
![Page 16: Lecture 13: Special Member Functionsweb.stanford.edu/class/cs106l/lectures/L13_Special...CS 106L, Fall ‘20 CS 106B covers the barebones of C++ classes we’ll be covering the rest](https://reader035.fdocuments.us/reader035/viewer/2022071407/60fe26c3501e8d22bb2d7fcd/html5/thumbnails/16.jpg)
Copy constructor - vec created as a copy of another one
vector<int> function(vector<int> vec0) { vector<int> vec1; vector<int> vec2(3); vector<int> vec3{3}; vector<int> vec4(); vector<int> vec5(vec2); vector<int> vec6{}; vector<int> vec7{vec3 + vec4}; vector<int> vec8 = vec4; vec8 = vec2; return vec8;}
● Default construction: object created with no parameters.
● Copy construction: object is created as a copy of an existing object.
● Copy assignment: existing object replaced as a copy of another existing object.
● Destruction: object destroyed when it is out of scope.
![Page 17: Lecture 13: Special Member Functionsweb.stanford.edu/class/cs106l/lectures/L13_Special...CS 106L, Fall ‘20 CS 106B covers the barebones of C++ classes we’ll be covering the rest](https://reader035.fdocuments.us/reader035/viewer/2022071407/60fe26c3501e8d22bb2d7fcd/html5/thumbnails/17.jpg)
Also the default constructor!
vector<int> function(vector<int> vec0) { vector<int> vec1; vector<int> vec2(3); vector<int> vec3{3}; vector<int> vec4(); vector<int> vec5(vec2); vector<int> vec6{}; vector<int> vec7{vec3 + vec4}; vector<int> vec8 = vec4; vec8 = vec2; return vec8;}
● Default construction: object created with no parameters.
● Copy construction: object is created as a copy of an existing object.
● Copy assignment: existing object replaced as a copy of another existing object.
● Destruction: object destroyed when it is out of scope.
![Page 18: Lecture 13: Special Member Functionsweb.stanford.edu/class/cs106l/lectures/L13_Special...CS 106L, Fall ‘20 CS 106B covers the barebones of C++ classes we’ll be covering the rest](https://reader035.fdocuments.us/reader035/viewer/2022071407/60fe26c3501e8d22bb2d7fcd/html5/thumbnails/18.jpg)
Copy constructor
vector<int> function(vector<int> vec0) { vector<int> vec1; vector<int> vec2(3); vector<int> vec3{3}; vector<int> vec4(); vector<int> vec5(vec2); vector<int> vec6{}; vector<int> vec7{vec3 + vec4}; vector<int> vec8 = vec4; vec8 = vec2; return vec8;}
● Default construction: object created with no parameters.
● Copy construction: object is created as a copy of an existing object.
● Copy assignment: existing object replaced as a copy of another existing object.
● Destruction: object destroyed when it is out of scope.
![Page 19: Lecture 13: Special Member Functionsweb.stanford.edu/class/cs106l/lectures/L13_Special...CS 106L, Fall ‘20 CS 106B covers the barebones of C++ classes we’ll be covering the rest](https://reader035.fdocuments.us/reader035/viewer/2022071407/60fe26c3501e8d22bb2d7fcd/html5/thumbnails/19.jpg)
Copy constructor - vec8 is newly constructed
vector<int> function(vector<int> vec0) { vector<int> vec1; vector<int> vec2(3); vector<int> vec3{3}; vector<int> vec4(); vector<int> vec5(vec2); vector<int> vec6{}; vector<int> vec7{vec3 + vec4}; vector<int> vec8 = vec4; vec8 = vec2; return vec8;}
● Default construction: object created with no parameters.
● Copy construction: object is created as a copy of an existing object.
● Copy assignment: existing object replaced as a copy of another existing object.
● Destruction: object destroyed when it is out of scope.
![Page 20: Lecture 13: Special Member Functionsweb.stanford.edu/class/cs106l/lectures/L13_Special...CS 106L, Fall ‘20 CS 106B covers the barebones of C++ classes we’ll be covering the rest](https://reader035.fdocuments.us/reader035/viewer/2022071407/60fe26c3501e8d22bb2d7fcd/html5/thumbnails/20.jpg)
Copy assignment - vec8 is an existing object
vector<int> function(vector<int> vec0) { vector<int> vec1; vector<int> vec2(3); vector<int> vec3{3}; vector<int> vec4(); vector<int> vec5(vec2); vector<int> vec6{}; vector<int> vec7{vec3 + vec4}; vector<int> vec8 = vec4; vec8 = vec2; return vec8;}
● Default construction: object created with no parameters.
● Copy construction: object is created as a copy of an existing object.
● Copy assignment: existing object replaced as a copy of another existing object.
● Destruction: object destroyed when it is out of scope.
![Page 21: Lecture 13: Special Member Functionsweb.stanford.edu/class/cs106l/lectures/L13_Special...CS 106L, Fall ‘20 CS 106B covers the barebones of C++ classes we’ll be covering the rest](https://reader035.fdocuments.us/reader035/viewer/2022071407/60fe26c3501e8d22bb2d7fcd/html5/thumbnails/21.jpg)
Copy constructor: copies vec8 to location outside of func
vector<int> function(vector<int> vec0) { vector<int> vec1; vector<int> vec2(3); vector<int> vec3{3}; vector<int> vec4(); vector<int> vec5(vec2); vector<int> vec6{}; vector<int> vec7{vec3 + vec4}; vector<int> vec8 = vec4; vec8 = vec2; return vec8;}
● Default construction: object created with no parameters.
● Copy construction: object is created as a copy of an existing object.
● Copy assignment: existing object replaced as a copy of another existing object.
● Destruction: object destroyed when it is out of scope.
![Page 22: Lecture 13: Special Member Functionsweb.stanford.edu/class/cs106l/lectures/L13_Special...CS 106L, Fall ‘20 CS 106B covers the barebones of C++ classes we’ll be covering the rest](https://reader035.fdocuments.us/reader035/viewer/2022071407/60fe26c3501e8d22bb2d7fcd/html5/thumbnails/22.jpg)
Destructors on all values (except return value) are called
vector<int> function(vector<int> vec0) { vector<int> vec1; vector<int> vec2(3); vector<int> vec3{3}; vector<int> vec4(); vector<int> vec5(vec2); vector<int> vec6{}; vector<int> vec7{vec3 + vec4}; vector<int> vec8 = vec4; vec8 = vec2; return vec8;}
● Default construction: object created with no parameters.
● Copy construction: object is created as a copy of an existing object.
● Copy assignment: existing object replaced as a copy of another existing object.
● Destruction: object destroyed when it is out of scope.
![Page 23: Lecture 13: Special Member Functionsweb.stanford.edu/class/cs106l/lectures/L13_Special...CS 106L, Fall ‘20 CS 106B covers the barebones of C++ classes we’ll be covering the rest](https://reader035.fdocuments.us/reader035/viewer/2022071407/60fe26c3501e8d22bb2d7fcd/html5/thumbnails/23.jpg)
🤔 Questions? 🤔
![Page 24: Lecture 13: Special Member Functionsweb.stanford.edu/class/cs106l/lectures/L13_Special...CS 106L, Fall ‘20 CS 106B covers the barebones of C++ classes we’ll be covering the rest](https://reader035.fdocuments.us/reader035/viewer/2022071407/60fe26c3501e8d22bb2d7fcd/html5/thumbnails/24.jpg)
Member initializer lists
![Page 25: Lecture 13: Special Member Functionsweb.stanford.edu/class/cs106l/lectures/L13_Special...CS 106L, Fall ‘20 CS 106B covers the barebones of C++ classes we’ll be covering the rest](https://reader035.fdocuments.us/reader035/viewer/2022071407/60fe26c3501e8d22bb2d7fcd/html5/thumbnails/25.jpg)
How we’re used to writing constructors
template <typename T>vector<T>::vector<T>() { _size = 0; _capacity = kInitialSize; _elems = new T[kInitialSize];}
Members are first default constructed
Then each member is reassigned
![Page 26: Lecture 13: Special Member Functionsweb.stanford.edu/class/cs106l/lectures/L13_Special...CS 106L, Fall ‘20 CS 106B covers the barebones of C++ classes we’ll be covering the rest](https://reader035.fdocuments.us/reader035/viewer/2022071407/60fe26c3501e8d22bb2d7fcd/html5/thumbnails/26.jpg)
Member initializer lists!
template <typename T>vector<T>::vector<T>() { _size = 0; _capacity = kInitialSize; _elems = new T[kInitialSize];}
template <typename T>vector<T>::vector<T>() : _size(0), _capacity(kInitialSize), _elems(new T[kInitialSize]) { }
Directly construct each member with a starting value
![Page 27: Lecture 13: Special Member Functionsweb.stanford.edu/class/cs106l/lectures/L13_Special...CS 106L, Fall ‘20 CS 106B covers the barebones of C++ classes we’ll be covering the rest](https://reader035.fdocuments.us/reader035/viewer/2022071407/60fe26c3501e8d22bb2d7fcd/html5/thumbnails/27.jpg)
Quick summary
● Prefer to use member initializer lists, which directly constructs each member with a given value.○ Faster. Why construct, then immediately reassign?○ Members might be a non-assignable type (we’ll see by the end of lecture
how this can be possible!)● Important clarification: you can use member initializer lists for ANY
constructor, not just a special member function. This means you can use it even if your constructor has parameters.
![Page 28: Lecture 13: Special Member Functionsweb.stanford.edu/class/cs106l/lectures/L13_Special...CS 106L, Fall ‘20 CS 106B covers the barebones of C++ classes we’ll be covering the rest](https://reader035.fdocuments.us/reader035/viewer/2022071407/60fe26c3501e8d22bb2d7fcd/html5/thumbnails/28.jpg)
Why aren’t the default member functions always
sufficient?
![Page 29: Lecture 13: Special Member Functionsweb.stanford.edu/class/cs106l/lectures/L13_Special...CS 106L, Fall ‘20 CS 106B covers the barebones of C++ classes we’ll be covering the rest](https://reader035.fdocuments.us/reader035/viewer/2022071407/60fe26c3501e8d22bb2d7fcd/html5/thumbnails/29.jpg)
The default compiler-generated copy constructor and copy assignment functions
work by copying each member variable
![Page 30: Lecture 13: Special Member Functionsweb.stanford.edu/class/cs106l/lectures/L13_Special...CS 106L, Fall ‘20 CS 106B covers the barebones of C++ classes we’ll be covering the rest](https://reader035.fdocuments.us/reader035/viewer/2022071407/60fe26c3501e8d22bb2d7fcd/html5/thumbnails/30.jpg)
This is what default copy constructor would look like
template <typename T>vector::vector<T>(const vector<T>& other) : _size(other._size), _capacity(other._capacity), _elems(other._elems) { }
![Page 31: Lecture 13: Special Member Functionsweb.stanford.edu/class/cs106l/lectures/L13_Special...CS 106L, Fall ‘20 CS 106B covers the barebones of C++ classes we’ll be covering the rest](https://reader035.fdocuments.us/reader035/viewer/2022071407/60fe26c3501e8d22bb2d7fcd/html5/thumbnails/31.jpg)
Consider the following code
vector<int> operator+(const vector<int>& vec, int elem) { vector<int> copy = vec; // uses default copy constructor copy += element; // assumes we’ve defined += operator return copy;}
This code, sadly, does not work!
![Page 32: Lecture 13: Special Member Functionsweb.stanford.edu/class/cs106l/lectures/L13_Special...CS 106L, Fall ‘20 CS 106B covers the barebones of C++ classes we’ll be covering the rest](https://reader035.fdocuments.us/reader035/viewer/2022071407/60fe26c3501e8d22bb2d7fcd/html5/thumbnails/32.jpg)
Both copy and vec will point to the same underlying array!
vector<int> operator+(const vector<int>& vec, int elem) { vector<int> copy = vec; copy += element; return copy;}
1 2 3 4 5
copy
vec
![Page 33: Lecture 13: Special Member Functionsweb.stanford.edu/class/cs106l/lectures/L13_Special...CS 106L, Fall ‘20 CS 106B covers the barebones of C++ classes we’ll be covering the rest](https://reader035.fdocuments.us/reader035/viewer/2022071407/60fe26c3501e8d22bb2d7fcd/html5/thumbnails/33.jpg)
The culprit? This line in the default copy constructor!
template <typename T>vector::vector<T>(const vector<T>& other) : _size(other._size, _capacity(other._capacity), _elems(other._elems) { }
Remember, _elems is a pointer, so this line makes a copy of a pointer, which isn’t the same as copying the underlying array!
![Page 34: Lecture 13: Special Member Functionsweb.stanford.edu/class/cs106l/lectures/L13_Special...CS 106L, Fall ‘20 CS 106B covers the barebones of C++ classes we’ll be covering the rest](https://reader035.fdocuments.us/reader035/viewer/2022071407/60fe26c3501e8d22bb2d7fcd/html5/thumbnails/34.jpg)
The culprit? This line in the default copy assignment!
template <typename T>vector::vector<T>(const vector<T>& other) : _size(other._size, _capacity(other._capacity), _elems(other._elems) { }
This is because when you copy a pointer, you copy the address saved in the pointer, not what’s being pointed to!
![Page 35: Lecture 13: Special Member Functionsweb.stanford.edu/class/cs106l/lectures/L13_Special...CS 106L, Fall ‘20 CS 106B covers the barebones of C++ classes we’ll be covering the rest](https://reader035.fdocuments.us/reader035/viewer/2022071407/60fe26c3501e8d22bb2d7fcd/html5/thumbnails/35.jpg)
Moral of the story: in many cases, copying is not as simple as copying each member
variable
![Page 36: Lecture 13: Special Member Functionsweb.stanford.edu/class/cs106l/lectures/L13_Special...CS 106L, Fall ‘20 CS 106B covers the barebones of C++ classes we’ll be covering the rest](https://reader035.fdocuments.us/reader035/viewer/2022071407/60fe26c3501e8d22bb2d7fcd/html5/thumbnails/36.jpg)
This is one example of when you might want to overwrite the default special
member functions with your own implementation!
![Page 37: Lecture 13: Special Member Functionsweb.stanford.edu/class/cs106l/lectures/L13_Special...CS 106L, Fall ‘20 CS 106B covers the barebones of C++ classes we’ll be covering the rest](https://reader035.fdocuments.us/reader035/viewer/2022071407/60fe26c3501e8d22bb2d7fcd/html5/thumbnails/37.jpg)
🤔 Questions? 🤔
![Page 38: Lecture 13: Special Member Functionsweb.stanford.edu/class/cs106l/lectures/L13_Special...CS 106L, Fall ‘20 CS 106B covers the barebones of C++ classes we’ll be covering the rest](https://reader035.fdocuments.us/reader035/viewer/2022071407/60fe26c3501e8d22bb2d7fcd/html5/thumbnails/38.jpg)
Copy operations: fixing the issues we just saw
![Page 39: Lecture 13: Special Member Functionsweb.stanford.edu/class/cs106l/lectures/L13_Special...CS 106L, Fall ‘20 CS 106B covers the barebones of C++ classes we’ll be covering the rest](https://reader035.fdocuments.us/reader035/viewer/2022071407/60fe26c3501e8d22bb2d7fcd/html5/thumbnails/39.jpg)
Recap about definitions
● Default construction: object created with no parameters.
● Copy construction: object is created as a copy of an existing object.
● Copy assignment: existing object replaced as a copy of another existing object.
● Destruction: object destroyed when it is out of scope.
![Page 40: Lecture 13: Special Member Functionsweb.stanford.edu/class/cs106l/lectures/L13_Special...CS 106L, Fall ‘20 CS 106B covers the barebones of C++ classes we’ll be covering the rest](https://reader035.fdocuments.us/reader035/viewer/2022071407/60fe26c3501e8d22bb2d7fcd/html5/thumbnails/40.jpg)
The copy operations must perform the following tasks
Copy constructor
• Use initializer list to copy members where copy constructor does the correct thing.
• int, other objects, etc.• Manually copy all members
where assignment does not work.• pointers to heap memory• non-copyable things
Copy assignment
• Clean up any resources in the existing object about to be overwritten.
• Copy members using direct assignment when assignment works.
• Manually copy members where assignment does not work.
![Page 41: Lecture 13: Special Member Functionsweb.stanford.edu/class/cs106l/lectures/L13_Special...CS 106L, Fall ‘20 CS 106B covers the barebones of C++ classes we’ll be covering the rest](https://reader035.fdocuments.us/reader035/viewer/2022071407/60fe26c3501e8d22bb2d7fcd/html5/thumbnails/41.jpg)
How do we fix the default copy constructor? (chat)
template <typename T>vector::vector<T>(const vector<T>& other) : _size(other._size, _capacity(other._capacity), _elems(other._elems) {
}
![Page 42: Lecture 13: Special Member Functionsweb.stanford.edu/class/cs106l/lectures/L13_Special...CS 106L, Fall ‘20 CS 106B covers the barebones of C++ classes we’ll be covering the rest](https://reader035.fdocuments.us/reader035/viewer/2022071407/60fe26c3501e8d22bb2d7fcd/html5/thumbnails/42.jpg)
Here’s a fix: we can create a new array!
template <typename T>vector::vector<T>(const vector<T>& other) : _size(other._size, _capacity(other._capacity), _elems(other._elems) {
_elems = new T[other._capacity];std::copy(other._elems,
other._elems + other._size, _elems);}
![Page 43: Lecture 13: Special Member Functionsweb.stanford.edu/class/cs106l/lectures/L13_Special...CS 106L, Fall ‘20 CS 106B covers the barebones of C++ classes we’ll be covering the rest](https://reader035.fdocuments.us/reader035/viewer/2022071407/60fe26c3501e8d22bb2d7fcd/html5/thumbnails/43.jpg)
Even better: let’s move things to the initializer list!
template <typename T>vector::vector<T>(const vector<T>& other) : _size(other._size, _capacity(other._capacity), _elems(new T[other._capacity]) {
std::copy(other._elems,other._elems + other._size, _elems);
}
![Page 44: Lecture 13: Special Member Functionsweb.stanford.edu/class/cs106l/lectures/L13_Special...CS 106L, Fall ‘20 CS 106B covers the barebones of C++ classes we’ll be covering the rest](https://reader035.fdocuments.us/reader035/viewer/2022071407/60fe26c3501e8d22bb2d7fcd/html5/thumbnails/44.jpg)
How do we fix the default copy assignment operator?
template <typename T>vector<T>& vector<T>::operator=(const vector<T>& other) { _size = other._size; _capacity = other._capacity; _elems = other._elems;}
![Page 45: Lecture 13: Special Member Functionsweb.stanford.edu/class/cs106l/lectures/L13_Special...CS 106L, Fall ‘20 CS 106B covers the barebones of C++ classes we’ll be covering the rest](https://reader035.fdocuments.us/reader035/viewer/2022071407/60fe26c3501e8d22bb2d7fcd/html5/thumbnails/45.jpg)
Attempt 1: Allocate a new array and copy over elements
template <typename T>vector<T>& vector<T>::operator=(const vector<T>& other) { _size = other._size; _capacity = other._capacity; _elems = new T[other._capacity];
std::copy(other._elems, other._elems + other._size, _elems);}
![Page 46: Lecture 13: Special Member Functionsweb.stanford.edu/class/cs106l/lectures/L13_Special...CS 106L, Fall ‘20 CS 106B covers the barebones of C++ classes we’ll be covering the rest](https://reader035.fdocuments.us/reader035/viewer/2022071407/60fe26c3501e8d22bb2d7fcd/html5/thumbnails/46.jpg)
There’s a problem here with memory leaks!
template <typename T>vector<T>& vector<T>::operator=(const vector<T>& other) { _size = other._size; _capacity = other._capacity; _elems = new T[other._capacity];
std::copy(other._elems, other._elems + other._size, _elems);}
Remember, we’re changing the concepts of an existing object. What about the old
array of elements that _elems pointed to?
![Page 47: Lecture 13: Special Member Functionsweb.stanford.edu/class/cs106l/lectures/L13_Special...CS 106L, Fall ‘20 CS 106B covers the barebones of C++ classes we’ll be covering the rest](https://reader035.fdocuments.us/reader035/viewer/2022071407/60fe26c3501e8d22bb2d7fcd/html5/thumbnails/47.jpg)
Attempt 2: Use a temporary pointer
template <typename T>vector<T>& vector<T>::operator=(const vector<T>& other) { _size = other._size; _capacity = other._capacity; T* new_elems = new T[other._capacity];
std::copy(other._elems, other._elems + other._size, new_elems); delete [] _elems; _elems = new_elems;}
![Page 48: Lecture 13: Special Member Functionsweb.stanford.edu/class/cs106l/lectures/L13_Special...CS 106L, Fall ‘20 CS 106B covers the barebones of C++ classes we’ll be covering the rest](https://reader035.fdocuments.us/reader035/viewer/2022071407/60fe26c3501e8d22bb2d7fcd/html5/thumbnails/48.jpg)
Minor detail 1: return reference to vector itself
template <typename T>vector<T>& vector<T>::operator=(const vector<T>& other) { _size = other._size; _capacity = other._capacity; T* new_elems = new T[other._capacity];
std::copy(other._elems, other._elems + other._size, new_elems); delete [] _elems; _elems = new_elems; return *this;}
![Page 49: Lecture 13: Special Member Functionsweb.stanford.edu/class/cs106l/lectures/L13_Special...CS 106L, Fall ‘20 CS 106B covers the barebones of C++ classes we’ll be covering the rest](https://reader035.fdocuments.us/reader035/viewer/2022071407/60fe26c3501e8d22bb2d7fcd/html5/thumbnails/49.jpg)
Minor detail 2: Be careful about self-reassignment
template <typename T>vector<T>& vector<T>::operator=(const vector<T>& other) { if (&other == this) return *this; _size = other._size; _capacity = other._capacity; T* new_elems = new T[other._capacity];
std::copy(other._elems, other._elems + other._size, new_elems); delete [] _elems; _elems = new_elems; return *this;}
![Page 50: Lecture 13: Special Member Functionsweb.stanford.edu/class/cs106l/lectures/L13_Special...CS 106L, Fall ‘20 CS 106B covers the barebones of C++ classes we’ll be covering the rest](https://reader035.fdocuments.us/reader035/viewer/2022071407/60fe26c3501e8d22bb2d7fcd/html5/thumbnails/50.jpg)
Summary: Steps to follow for an assignment operator
1. Check for self-assignment.2. Make sure to free existing members if applicable.3. Copy assign each automatically assignable member.4. Manually copy all other members.5. Return a reference to *this (that was just reassigned).
![Page 51: Lecture 13: Special Member Functionsweb.stanford.edu/class/cs106l/lectures/L13_Special...CS 106L, Fall ‘20 CS 106B covers the barebones of C++ classes we’ll be covering the rest](https://reader035.fdocuments.us/reader035/viewer/2022071407/60fe26c3501e8d22bb2d7fcd/html5/thumbnails/51.jpg)
🤔 Questions? 🤔
![Page 52: Lecture 13: Special Member Functionsweb.stanford.edu/class/cs106l/lectures/L13_Special...CS 106L, Fall ‘20 CS 106B covers the barebones of C++ classes we’ll be covering the rest](https://reader035.fdocuments.us/reader035/viewer/2022071407/60fe26c3501e8d22bb2d7fcd/html5/thumbnails/52.jpg)
Delete
![Page 53: Lecture 13: Special Member Functionsweb.stanford.edu/class/cs106l/lectures/L13_Special...CS 106L, Fall ‘20 CS 106B covers the barebones of C++ classes we’ll be covering the rest](https://reader035.fdocuments.us/reader035/viewer/2022071407/60fe26c3501e8d22bb2d7fcd/html5/thumbnails/53.jpg)
How could you prevent copies from being created?
class PasswordManager { public: PasswordManager(); ~PasswordManager(); // other methods PasswordManager(const PasswordManager& rhs) = delete; PasswordManager& operator=(const PasswordManager& rhs) = delete;
private: // other stuff}
Explicitly delete the copy member functions!
![Page 54: Lecture 13: Special Member Functionsweb.stanford.edu/class/cs106l/lectures/L13_Special...CS 106L, Fall ‘20 CS 106B covers the barebones of C++ classes we’ll be covering the rest](https://reader035.fdocuments.us/reader035/viewer/2022071407/60fe26c3501e8d22bb2d7fcd/html5/thumbnails/54.jpg)
Rule of three
![Page 55: Lecture 13: Special Member Functionsweb.stanford.edu/class/cs106l/lectures/L13_Special...CS 106L, Fall ‘20 CS 106B covers the barebones of C++ classes we’ll be covering the rest](https://reader035.fdocuments.us/reader035/viewer/2022071407/60fe26c3501e8d22bb2d7fcd/html5/thumbnails/55.jpg)
When to define your own special member functions?
● When the default ones generated by the compiler won’t work● Most common reason: ownership issues
○ A member is a handle on a resource outside of the class○ E.g. pointers to dynamically allocated memory, mutexes, etc.
![Page 56: Lecture 13: Special Member Functionsweb.stanford.edu/class/cs106l/lectures/L13_Special...CS 106L, Fall ‘20 CS 106B covers the barebones of C++ classes we’ll be covering the rest](https://reader035.fdocuments.us/reader035/viewer/2022071407/60fe26c3501e8d22bb2d7fcd/html5/thumbnails/56.jpg)
Rule of three
● If you explicitly define (or delete) a copy constructor, copy assignment, or destructor, you should define (or delete) all three.
● What’s the rationale?
![Page 57: Lecture 13: Special Member Functionsweb.stanford.edu/class/cs106l/lectures/L13_Special...CS 106L, Fall ‘20 CS 106B covers the barebones of C++ classes we’ll be covering the rest](https://reader035.fdocuments.us/reader035/viewer/2022071407/60fe26c3501e8d22bb2d7fcd/html5/thumbnails/57.jpg)
Rule of three
● If you explicitly define a copy constructor, copy assignment, or destructor, you should define all three.
● What’s the rationale?○ The fact that you defined one of these means one of your members has
ownership issues that need to be resolved.
![Page 58: Lecture 13: Special Member Functionsweb.stanford.edu/class/cs106l/lectures/L13_Special...CS 106L, Fall ‘20 CS 106B covers the barebones of C++ classes we’ll be covering the rest](https://reader035.fdocuments.us/reader035/viewer/2022071407/60fe26c3501e8d22bb2d7fcd/html5/thumbnails/58.jpg)
Rule of 0
● If the default operations work, then don’t define your own custom ones
![Page 59: Lecture 13: Special Member Functionsweb.stanford.edu/class/cs106l/lectures/L13_Special...CS 106L, Fall ‘20 CS 106B covers the barebones of C++ classes we’ll be covering the rest](https://reader035.fdocuments.us/reader035/viewer/2022071407/60fe26c3501e8d22bb2d7fcd/html5/thumbnails/59.jpg)
Summary
● The C++ compiler is powerful enough to generate special member functions for us
● In some cases, we may need to redefine these functions if the default behavior does not match our desired behavior
● We can delete special member functions to make certain behavior impossible (e.g., make it impossible to copy an object of our class)
![Page 60: Lecture 13: Special Member Functionsweb.stanford.edu/class/cs106l/lectures/L13_Special...CS 106L, Fall ‘20 CS 106B covers the barebones of C++ classes we’ll be covering the rest](https://reader035.fdocuments.us/reader035/viewer/2022071407/60fe26c3501e8d22bb2d7fcd/html5/thumbnails/60.jpg)
🤔 Questions? 🤔
![Page 61: Lecture 13: Special Member Functionsweb.stanford.edu/class/cs106l/lectures/L13_Special...CS 106L, Fall ‘20 CS 106B covers the barebones of C++ classes we’ll be covering the rest](https://reader035.fdocuments.us/reader035/viewer/2022071407/60fe26c3501e8d22bb2d7fcd/html5/thumbnails/61.jpg)
Next time
Move semantics