title: Can a std::function member have access to other members?
body: <p>I am making a small tower defence game in order to practice C++. I know other languages, but have done little actual development in C++.
  In this game I'll have several buildings, so I made a class like so:</p>
  
  <pre><code>class Building
  {
  private:
      sf::Sprite&amp; sprite;
      float timer = 0;
      void update(float delta);
  };
  </code></pre>
  
  <p>Now, I'll have many different buildings, but they should all have the same members. Now, in Java I would (have to) make a subclass for each different type of building, bu I was thinking, can I change <code>void update(float delta);</code> to <code>std::function&lt;void(float)&gt; update;</code> and have factory funtions to create the different buildings? What I imagine is something like this:</p>
  
  <p>In building.h:</p>
  
  <pre><code>class Building
  {
  private:
      sf::Sprite&amp; sprite;
      float timer = 0;
      std::function&lt;void(float)&gt; update;
  };
  </code></pre>
  
  <p>And in building.cpp</p>
  
  <pre><code>Building getBuildingA()
  {
      Building b;
      b.sprite = /* ... */
      b.update = [](float delta) {
          std::cout &lt;&lt; timer++ &lt;&lt; std::endl;
      };
      return b;
  }
  </code></pre>
  
  <p>But of course, I cannot actually access the variable timer there. Is there a way I can do something similar?</p>