January 2019
Intermediate to advanced
512 pages
14h 5m
English
Let's start with the most common kind of memory ownership. Most code does not allocate, deallocate, construct, or delete. It just does its work on objects that were created by someone else earlier and will be deleted by someone else later. How do you express the notion that a function is going to operate on an object but will not attempt to delete it or, conversely, extend its lifetime past the completion of the function itself?
Very easily, in fact, and every C++ programmer has done it many times:
void Transmogrify(Widged* w) { // I will not delete w ...}void MustTransmogrify(Widget& w) { // Neither will I ...}class WidgetProcessor { public: WidgetProcessor(Widget* w) : w_(w) {} ... Widget* w_; // I do not own w ...