liveBook · Manning

Immutable collection

As explained in Object-Oriented Software Design in C++

An immutable object is an object whose observable state cannot be changed after it is created.

An immutable object cannot have its observable state changed after creation. A class with no setter functions can keep its own state stable, but immutability also depends on preventing access to mutable internal objects. section 5.4 section 5.7

Protecting an employee birthdate

A faulty Employee stores the caller’s Date pointer and returns its internal pointer. The caller can then change the birthdate both before and after construction. The corrected design stores a Date copy and returns a new copy, so both attempted changes leave the Employee unchanged. section 5.7

Check immutability by looking for these change paths:

  • Remove public setters when the application requires state to remain fixed. section 5.4
  • Copy mutable objects entering an immutable object instead of retaining caller-owned pointers. section 5.7
  • Return copies rather than pointers into hidden mutable state. section 5.7
Listing 5.15 (Program 5.8 HiddenDate-1): Employee.h (faulty design)
#include <iostream>
#include <string>
#include "Date.h"

using namespace std;

class Employee
{
public:
    Employee(long id, string name, Date *bdate)
        : employee_id(id), name(name), birthdate(bdate) {}

    Date *get_birthdate() const { return birthdate; }

    friend ostream& operator <<(ostream& os, const Employee& emp);

private:
    long employee_id;
    string name;

    Date *birthdate;
};

inline ostream& operator <<(ostream& os, const Employee& emp)
{
    os << "Employee #" <<emp.employee_id << endl;
    os << "  Name: " << emp.name << endl;
    os << "  Birthdate: "; emp.birthdate->print(); os << endl;

    return os;
}

Inspect the faulty Employee class to see how pointer aliasing defeats immutability. section 5.7

How Object-Oriented Software Design in C++ covers it

How deep it goes
Descends from absent setters to pointer-based change leaks, then demonstrates copy-based remedies.
Examples
Uses Date objects and Employee birthdates, testing external and getter-returned pointers before and after copying.
What you can do after
Diagnose/debug with it by finding pointer-based change leaks and applying defensive copies at construction and access.
How it connects
Connects immutability with implementation hiding, encapsulation, getter functions, setter functions, and aggregation.

Also explained in

Compare how 24 books explain immutable collection →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Object-Oriented Software Design in C++. The section links point to the parts of the book it draws on.