title: Mutability design patterns in Objective C and C++
body: <p>Having recently done some development for iPhone, I've come to notice an interesting design pattern used a lot in the iPhone SDK, regarding object mutability.</p>
  
  <p>It seems the typical approach there is to define an immutable class <code>NSFoo</code>, and then derive from it a mutable descendant <code>NSMutableFoo</code>. Generally, the <code>NSFoo</code> class defines data members, getters and read-only operations, and the derived <code>NSMutableFoo</code> adds on setters and mutating operations.</p>
  
  <p>Being more familiar with C++, I couldn't help but notice that this seems to be a complete opposite to what I'd do when writing the same code in C++. While you certainly <em>could</em> take that approach, it seems to me that a more concise approach is to create a single <code>Foo</code> class, mark getters and read-only operations as <code>const</code> functions, and also implement the mutable operations and setters in the same class. You would then end up with a mutable class, but the types <code>Foo const*</code>, <code>Foo const&amp;</code> etc all are effectively the immutable equivalent.</p>
  
  <p>I guess my question is, does my take on the situation make sense? I understand why Objective-C does things differently, but are there any advantages to the two-class approach in C++ that I've missed? Or am I missing the point entirely?</p>
  
  <p>Not an overly serious question - more for my own curiosity than anything else.</p>