teaching machines

CS 330 Lecture 38 – Dealing with Temporaries

May 6, 2016 by . Filed under cs330, lectures, spring 2016.

Agenda

TODO

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:

  1. Claim ownership of the source object’s data.
  2. 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 `
'