CS 330 Lecture 38 – Dealing with Temporaries
Agenda
- what ?s
- fixing output issues
- read constructor
- splitting images with
|
- move semantics
TODO
- One more 2-participation point extra credit opportunity for those of you who couldn’t make the evening talks: make two models in Madeup. The objects must be reasonably interesting and not just random globs of triangles. At least one must be an everyday object that you “reverse engineer” into an algorithm. Find documentation under the About menu. Email me the Madeup source code before the Monday of finals week.
Note
Today we start with a brief reading from The Design and Evolution of C++ of Stroustrup’s philosophical leanings that shaped the C++ language.
Then we finish up our twoface example that we’ve been using to discuss object-oriented programming. We’ll fix the issue we ended on last time with our file output. We’ll add a second constructor for reading in images, and at long last, we’ll be able to split-and-merge two images. We’ll overload the |
operator to accomplish this, but we’ll encounter an issue that pervades most programming languages: how do we transfer local function data back to the caller? We have several options to choose from: invoke a copy constructor, dynamically allocate the local data, or invoke move semantics.
Move semantics were introduced in C++11. We must write a move constructor, which is like a copy constructor. We can summarize the responsibilities of a move constructor:
- Claim ownership of the source object’s data.
- Null out any dynamically allocated memory in source object that would be released by the constructor.
Code
twoface.cpp
/usr/lib/ruby/2.7.0/rubygems/dependency.rb:311:in `to_specs': Could not find 'coderay' (>= 0) among 56 total gem(s) (Gem::MissingSpecError)
Checked in 'GEM_PATH=/.gem/ruby/2.7.0:/var/lib/gems/2.7.0:/usr/lib/ruby/gems/2.7.0:/usr/share/rubygems-integration/2.7.0:/usr/share/rubygems-integration/all:/usr/lib/x86_64-linux-gnu/rubygems-integration/2.7.0:/home/johnch/.gems', execute `gem env` for more information
from /usr/lib/ruby/2.7.0/rubygems/dependency.rb:323:in `to_spec'
from /usr/lib/ruby/2.7.0/rubygems/core_ext/kernel_gem.rb:62:in `gem'
from ./coderay:24:in `'