title: How do I access a private constructor in a separate class?
body: <p>I'm writing a library in C++.  I have two classes in my library, <code>A</code> and <code>B</code>.  I want to hide the <code>A()</code> constructor from any code that references my library.  I also want class <code>B</code> to be able to call the <code>A()</code> constructor.</p>
  
  <p>I come from a C# background and remember little of my C++.  In C#, I would simply declare the <code>A()</code> constructor as <code>internal</code>.  I've read that the closest way to do this in C++ is a combination of <code>friend</code> declarations and forward-declarations.  How do I do this?  Here are my three files below:</p>
  
  <p>A.h:</p>
  
  <pre><code>#pragma once
  class A
  {
      private:
          A();
  };
  </code></pre>
  
  <hr>
  
  <p>B.h</p>
  
  <pre><code>#pragma once
  class A;
  class B
  {
      public:
          A createA();
  };
  </code></pre>
  
  <hr>
  
  <p>B.cpp:</p>
  
  <pre><code>#include "A.h"
  #include "B.h"
  
  A B::createA()
  {
      A result; //cannot access private member declare in class 'A'
      return result;
  }
  </code></pre>
  
  <hr>
  
  <p>I've tried adding this to A.h:</p>
  
  <pre><code>public: friend A createA();
  </code></pre>
  
  <p>I've instead tried adding this to A.h with a corresponding forward declaration:</p>
  
  <pre><code>public: friend A B::createA();
  </code></pre>
  
  <p>I've instead tried adding and <code>extern class B;</code> to A.h and making B a class like this:</p>
  
  <pre><code>public: friend class B;
  </code></pre>
  
  <p>I'm at a loss.</p>
  
  <p>I think this might be easier if I have the <code>B::createA()</code> function return a pointer to an <code>A</code> object rather than an <code>A</code> object directly, but that won't do in my case.  I am emulating a closed API and the API call returns an <code>A</code> object rather than a pointer.</p>