13. Lesson 2: Dynamic Allocation
• Objects have scope, and go away when execution reaches
the end of that scope
void f()
{
NamedRectangle fred_rectangle{ "Fred", 3, 4 };
// . . .
}
• What if other code needed that object still?
14. The Free Store
• Separate area of memory
• Objects created there stick around until deliberately cleaned
up
• To create one, use new – returns a pointer
• To clean up, use delete
18. Exceptions
• Sometimes what you want can’t be done
– That file doesn’t exist any more
– You don’t have permission to do that to the system
• Checking in advance is best, but not always possible
• Code that calls a function doesn’t need to check return value
• C++ provides a non ignorable signal that something awful
has happened
• Will return from any nested level of function calls to a place
that can handle the error
24. Copying, Assignment, and Destruction
• Copying a class-type object invokes a special constructor
– Called the copy constructor
– Rectangle(Rectangle const&);
– If you don’t define one, the compiler provides one by default
• The default behaviour is to copy each base and data
member
25. Copying, Assignment, and Destruction
• Copying a class-type object invokes a special constructor
– Called the copy constructor
– Rectangle(Rectangle const&);
– If you don’t define one, the compiler provides one by default
• The default behaviour is to copy each base and data
member
Rectangle(Rectangle const& other)
: _width{other._width}, _height{other._height}
26. Copying, Assignment, and Destruction
• Assigning to a class-type object invokes a special operator
– Called the assignment operator
– Rectangle& operator=(Rectangle const&);
– If you don’t define one, the compiler provides one by default
• The default behaviour is to assign each base and data
member
27. Copying, Assignment, and Destruction
• Assigning to a class-type object invokes a special operator
– Called the assignment operator
– Rectangle& operator=(Rectangle const&);
– If you don’t define one, the compiler provides one by default
• The default behaviour is to assign each base and data member
Rectangle& operator=(Rectangle const& other)
{
_width = other._width;
_height = other._height;
return *this;
}
28. Copying, Assignment, and Destruction
void f()
{
int x{1}; // x comes into existence
int y{2}; // y comes into existence
}
// y goes out of existence
// x goes out of existence
29. Destructor
• Name is always ~ plus the name of the class
– E.g., ~Rectangle()
• Returns nothing, takes no parameters
• Place to cleanup resources used by an object
– Close a file
– Release a lock
– Etc
• When exceptions transfer execution out of a scope,
everything declared in that scope is cleaned up
– Destructor runs
31. Smart pointers
• unique_ptr if only one object needs access to the underlying
pointer
• shared_ptr if several want to use the same underlying pointer
– Cleaned up when the last copy goes out of scope
• In <memory> header file
• If you’re using new or delete, you’re doing it wrong
32. RAII in General
• Constructor acquires resource
– Eg opens file
• All other member functions know resource is acquired
– Do not need to test and make sure
• Destructor releases resource
– Works even in the presence of exceptions