title: Pointers or references for dynamically allocated members that always exist?
body: <p>I have a class <code>CContainer</code> that has some members <code>CMemberX</code>, <code>CMemberY</code>, which are independent of each other and other <code>CClientA</code>, <code>CClientB</code> classes that use <code>CContainer</code>.</p>
  
  <pre><code>#include "MemberX.h"
  #include "MemberY.h"
  
  class CContainer
  {
  public:
      CMemberX &amp; GetX() const { return m_x; }
      CMemberY &amp; GetY() const { return m_y; }
  
  private:
      CMemberX m_x;
      CMemberY m_y;
  };
  </code></pre>
  
  <p>I want to avoid having to recompile all <code>CClient</code> classes when modifying one of the <code>CMember</code> classes using forward declarations and dynamic allocation of <code>m_x</code> and <code>m_y</code>.</p>
  
  <p>Initially, I made the members pointers:</p>
  
  <pre><code>// Container.h
  class CMemberX;
  class CMemberY;
  
  class CContainer
  {
  public:
      CContainer();
      ~CContainer();
  
      CMemberX &amp; GetX() const { ASSERT(m_pX != NULL); return *m_pX; }
      CMemberY &amp; GetY() const { ASSERT(m_pY != NULL); return *m_pY; }
  
  private:
      CMemberX* m_pX;
      CMemberY* m_pY;
  };
  
  // Container.cpp
  #include "Container.h"
  #include "MemberX.h"
  #include "MemberY.h"
  
  // Allocate members on heap
  CContainer::CContainer() : m_pX(new CMemberX()), m_pY(new CMemberY()) {}
  CContainer::~CContainer() { delete m_pX; delete m_pY; }
  </code></pre>
  
  <p>Then I thought, that I could as well use references instead of pointers, so it looks more like the original code:</p>
  
  <pre><code>// Container.h
  class CMemberX;
  class CMemberY;
  
  class CContainer
  {
  public:
      CContainer();
      ~CContainer();
  
      CMemberX &amp; GetX() const { return m_x; }
      CMemberY &amp; GetY() const { return m_y; }
  
  private:
      CMemberX &amp; m_x;
      CMemberY &amp; m_y;
  };
  
  // Container.cpp
  #include "Container.h"
  #include "MemberX.h"
  #include "MemberY.h"
  
  // Allocate members on heap
  CContainer::CContainer() : m_x(*new CMemberX()), m_y(*new CMemberY()) {}
  CContainer::~CContainer() { delete &amp;m_x; delete &amp;m_y; }
  </code></pre>
  
  <p>What I don't like about the pointer members is that it <strong>looks</strong> like the pointers could be <code>NULL</code> or the objects be replaced at runtime, which is not the case.</p>
  
  <p>What I don't like about the references is that the code in the CTor and DTor looks a bit hacky.</p>
  
  <p>Which approach is preferable? Is there a better solution?</p>
  
  <p><strong>Note regarding copying/assigning:</strong> Instances of the <code>CContainer</code> class will not under any circumstances be copied or assigned to each other.</p>