title: C++ Lambda capture private class member
body: <p>Here is a part of my Class:</p>
  
  <pre><code>//...
          bool dump_dvars()
          {
              fstream&amp; refvar{ output_file };
  
              for_each(_array_start, _array_start + *_array_size,
                  [&amp;refvar](const void* dvar) -&gt; void
              {
                  //work with output_file
              });
  
              return true;
          }
  
      private:
          void** _array_start;
          unsigned int* _array_size;
          fstream output_file;
      };
  </code></pre>
  
  <p>I want to access the private member variable output_file of my Lambda, which is in the public member function dump_dvars. When I capture the this pointer I can not access the variable because it is private, I also don't want to make it public though! I already read this question (<a href="https://stackoverflow.com/questions/21646999/how-to-make-the-lambda-a-friend-of-a-class">How to make the lambda a friend of a class?</a>) but I don't want to create another function. So my current fix for the problem is creating a reference to the private member and pass that variable via reference capture list to my Lambda.</p>
  
  <p>Is that a good solution and good style or is there a better solution?</p>