title: use factory friend class to allocate new class, good practice?
body: <p>i have a complex class that i want to force the user to use std::unique_ptr for it, the way i see it the best way is to create a factory method that returns a std::unique_ptr and hide the ctor.</p>
  
  <p>problem is, the more factory methods i add the dirtier the header and code starts to look.</p>
  
  <p>so i thought maybe if i create a new ClassFactory class that has all the factory methods inside it, it would look cleaner and every time i want to add a new factory method i just edit the factory class and be done with it (leaving the original class clean).</p>
  
  <p>for example:</p>
  
  <pre><code>class Widget
  {
  private:
      friend class WidgetFactory;
  
      Widget(){}
      Widget(const Widget&amp; other) = delete;
      Widget&amp; operator = (const Widget&amp; other) = delete;
  
      std::string _msg;
  
  public:
      void printMessage(){ std::cout &lt;&lt; _msg &lt;&lt; std::endl; }
  };
  
  class WidgetFactory
  {
  public:
      static std::unique_ptr&lt;Widget&gt; create(){ auto w = std::unique_ptr&lt;Widget&gt;(new Widget);
                                               w-&gt;_msg = "hello world!"; return w; }
  };
  </code></pre>
  
  <p>and then the user:</p>
  
  <pre><code>int main(int argc, char** argv)
  {
      auto w = WidgetFactory::create();
      w-&gt;printMessage();
  }
  </code></pre>
  
  <p>another way is to make the ctor explicit public with a private token so i can use std::make_shared/unique.</p>
  
  <p>but i dont recall seeing this pattern before, so my question is, is this good practice? would you mind seeing code like this from your coworkers?</p>
  
  <p>thanks in advance.</p>