title: Singleton class whose ctor requires arguments
body: <p>I have class <code>Command</code> which is an instance of a class <code>EventManager</code>. Class Command requires two arguments(<code>host</code>, <code>target</code>) in its constructor.</p>
  
  <pre><code>class EventManager
  {
  public:
     void Event1(){ cmd-&gt;Execute(_eventEnum); }
  private:
     Command *cmd;
  };
  
  class Command
  {
  public:
     Command(Host h, Target t)
     void Execute();
  private:
  
  }
  </code></pre>
  
  <p>Now if i need to use this method <code>cmd-&gt;Execute()</code> in a member function of Target class, i need to make <code>cmd</code> an instance variable of Target or make it global as a singleton.</p>
  
  <p>I cant make <code>cmd</code> an instance variable of Target because it doesn't take host instance. To make it a singleton would be to add two methods like this</p>
  
  <pre><code>class Command
  {
  public:
  
     CreateInstance(Host h, Target t);
     GetInstance(); 
     void Execute();
  private:
     Command(Host h, Target t);
  
  }
  </code></pre>
  
  <p>I need to make sure that <code>GetInstance</code> is called after <code>CreateInstance</code>. Any other alternative? </p>
  
  <p>Target class is a low level class with few events. </p>
  
  <pre><code>Target::LowlevelEvent()
  {
   cmd-&gt;Execute(lowlevelevent) //In Execute for lowlevelevent in Command class, i might call target-&gt;reset
  }
  </code></pre>
  
  <p>Iam sorry for not being able to explain clearly. The problem is, this code has lot of events(methods), which may be in classes like EventManager or Target.</p>
  
  <p>In each of these event, i would have to call a Command->Execute(). The command class requires host and target instances because they can do certain actions.</p>
  
  <p>EventManager::Event1()
  {
   cmd->Execute(_event1);
  }</p>
  
  <p>Target::Event2()
  {
   cmd->Execute(_event2);
  }</p>
  
  <p>Command::Execute(Events e)
  {
   if (_event1 == e )
   {
     host->CallAction();
   }
   if (_event2 == e)
   {
     target->CallSomeOtherAction();
   }<br>
  }</p>
  
  <p>So now cmd needs to be an instance variable of both EventManager and Target right? Now EventManager has host and target instance which can be passed to Command ctor.</p>
  
  <p>But Target doesn't have access to host. So i cant create Command instance in Target class.</p>
  
  <p>So i was wondering if i create the singleton in EventManager ctor. And then call GetInstance() in Target. I know its bad idea but with this huge design, iam not able to figure out. Thanks for your help. </p>