title: Immutable classes in C++
body: <p>In one of my projects, I have some classes that represent entities that cannot change once created, aka. immutable classes.</p>
  
  <p>Example : A class <code>RSAKey</code> that represent a RSA key which only has const methods. There is no point changing the existing instance: if you need another one, you just create one.</p>
  
  <p>My objects sometimes are heavy and I enforced the use of smart pointers to avoid <strong>deep</strong> copy.</p>
  
  <p>So far, I have the following pattern for my classes:</p>
  
  <pre><code>class RSAKey : public boost::noncopyable, public boost::enable_shared_from_this&lt;RSAKey&gt;
  {
      public:
  
      /**
       * \brief Some factory.
       * \param member A member value.
       * \return An instance.
       */
      static boost::shared_ptr&lt;const RSAKey&gt; createFromMember(int member);
  
      /**
       * \brief Get a member.
       * \return The member.
       */
      int getMember() const;
  
      private:
  
      /**
       * \brief Constructor.
       * \param member A member.
       */
      RSAKey(int member);
  
      /**
       * \brief Member.
       */
      const int m_member;
  };
  </code></pre>
  
  <p>So you can only get a pointer (well, a <em>smart</em> pointer) to a const RSAKey. To me, it makes sense, because having a non-const reference to the instance is useless (it only has const methods).</p>
  
  <p>Do you guys see any issue regarding this pattern ? Are immutable classes something common in C++ or did I just created a monster ?</p>
  
  <p>Thank you for your advices !</p>