title: initialize a member unique_ptr to empty
body: <p>In my program, I have a bunch of objects of a custom class Position. The declaration of Position is as follows:</p>
  
  <pre><code>class Position {
  public:
      Position(int x, int y);
      ~Position();
  
      Actor *getActor()           { return actor.get(); };
      void setActor(Actor *actor) { actor = std::move(actor); };
      Actor *clearActor()         { return actor.release(); };
  
      int getX()  { return x; };
      int getY()  { return y; };
  
  private:
      int x, y;
      std::unique_ptr&lt;Actor&gt; actor;
  };
  </code></pre>
  
  <p>I also have a class called Actor. Not every Position will have an Actor, and so the majority of the time the unique_ptr "actor" of a Position object should be empty (I'm using unique_ptrs to automatically clean up any Actor associated with a Position at runtime). </p>
  
  <p>The Position constructor is as follows:</p>
  
  <pre><code>Position::Position(int x, int y)
  {
      this-&gt;x = x;
      this-&gt;y = y;
      actor.reset(nullptr);
  }
  </code></pre>
  
  <p>However, I know that this isn't correctly setting the stored pointer to nullptr because when I try calling actor.get() inside Position::getActor(), I get an error as follows:</p>
  
  <p><strong>First-chance exception at 0x01096486 in ____.exe: 0xC0000005: Access violation reading location 0x00000008.</strong></p>
  
  <p>Is there a way to initialize a member unique_ptr to nullptr? I know I could get around this by adding a variable to the Actor class that defines whether or not the Actor is active, setting the unique_ptr to a new inactive Actor, and ignoring all inactive Actors, but I'd rather avoid this if possible.</p>
  
  <p>Thanks!</p>
  
  <p>Edit: I've added the code where I call getActor:  </p>
  
  <pre><code>bool Grid::addActor(Actor *actor, int x, int y)
  {
      Position *destination = at(x, y);
  
      if (!destination-&gt;getActor()) {
          destination-&gt;setActor(actor);
          actor-&gt;setPosition(x, y);
          actor-&gt;setGrid(this);
          return true;
      }
      else {
          inactive_actors.emplace_back(actor);
          return false;
      }
  }
  </code></pre>