title: C++11 Capture Member Variable in Lambda
body: <p>I have a member variable defined as:</p>
  
  <p>HWND WindowHandle.</p>
  
  <p>I'm trying to capture the variable and assign to it in the Lambda. So the compiler gave me a warning and suggested that I capture "this". I did, but now the Handle is only valid within the Lambda :S Inother words, it is NULL outside of the Lambda.</p>
  
  <pre><code>class Foo
  {
      private:
          HWND WindowHandle;
  
      public:
          Foo();
          void MakeWindow(.......);
          HWND GetWindowHandle() {return WindowHandle;};
  };
  
  Foo::Foo(){}
  
  Foo::MakeWindow(.......)
  {
      Thread = std::thread([ClassName, Title, Width, Height, this]{
                      WindowHandle = CreateWindowEx(0, ClassName.c_str(), Title.c_str(), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, Width, Height, 0, 0, GetModuleHandle(NULL), 0);
                      if(WindowHandle)
                      {
                          ShowWindow(WindowHandle, SW_SHOWDEFAULT);
                          MSG msg;
                          while(GetMessage(&amp;msg, 0, 0, 0))
                              DispatchMessage(&amp;msg);
                      }
                  });
  }
  
  
  int main()
  {
      Foo F;
      F.MakeWindow(........);
      std::cout&lt;&lt;std::boolalpha&lt;&lt;(F.GetWindowHandle() == NULL);  //writes true.
  }
  </code></pre>
  
  <p>The above creates the window perfectly fine! It's just the Handle is null. How can I get the Handle from within the Lambda to my class Member?</p>