title: Lambda with inner class implementing an interface
body: <p>I am not very experienced with lambdas yet but I begin to like them very much and use them where it makes sense and where I feel they are the way to go.</p>
  
  <p>Anyway, I have a class <code>Tree</code> that has a <code>Tree::Visitor</code> class with one virtual callback function called <code>visit(/*args*/)</code>. That Visitor class does a recursive walk over all nodes. With this callback I am able to collect data from each node (or better I can extract paths of the tree (which is basically what I do with this function).</p>
  
  <p>So I take a lambda and inside I use a class to implement the <code>visit</code> callback function by deriving from <code>Tree::Visitor</code>.</p>
  
  <pre><code>// Tree class, a rough view how it looks
  class Tree {
  
      // ...
  
      // Visitor class for recursive walking the tree
      class Visitor {
         // 
         void count(/* ... */) {
             // in here the implemented visit(/*args*/) fct is called
         }
  
         // ...
         void triggerVisit() { 
             // ...
             count(/* ... */);
             // ...
         }
  
         // visitor callback
         virtual void visit(/* args */) = 0;
      };
  };
  
  class A {
      Tree tree;
      PriorityQueue que;
  
      A() : tree(), que(maxEntries) {}
  
      // first build the tree ...
      void buildTheTree() {
          tree.buildTree();               
      }
  
      // walk the tree
      void visitTheTree() {
  
         std::shared_ptr&lt;Tree::Visitor&gt;(
            [&amp;]()-&gt;Tree::Visitor * {
  
               // this class implements visit(/*args*/)
               class MyVisitor : public Tree::Visitor {
                   A&amp; parent; // pointer to A
  
                   Myvisitor(A&amp; p) 
                   : Tree::Visitor(p.tree), parent(p) {}      
  
                   // implementation
                   virtual void visit( /* args */ ) {
  
                       // ... get somedata 
  
                       if (/* condition true */) {
                           parent.que.push(somedata);
                       }
                   }  
               };
  
               return new MyVisitor(*this);
  
            }()
         )-&gt;triggerVisit();
  
         // get the collected data from que
         while(que.size() &gt; 0) {
            // ...
         }
      }
  };
  </code></pre>
  
  <p>Basically this is what I have and it is working without problems. </p>
  
  <p>I have a priority queue <code>que</code> that I use to store <code>somedata</code>, that are the <code>n</code> top scored nodes of the tree. At this time that <code>que</code> is defined as a member of class <code>A</code>, which I dislike, because I just need to collect the data inside the visitTheTree member, so it could be rather a local variable
  So my question is more a question of design/style and I have the feeling I miss something with the c++11 standard (maybe).</p>
  
  <p>I tried to define <code>que</code> inside <code>visitTheTree()</code> and pass it with the constructor of <code>MyVisitor</code>. Somehow this is not working correctly, at least I do not get proper/complete results I expect. When I define the Priority queue variable as a member of A (as it is now) and access it with the parent pointer in MyVistor, I get proper results and all is fine.</p>
  
  <p>Is there any good way to define <code>que</code> locally in VisitTheTree() instead of defining it in the class A as a member? I know I have to pass it with the constructor as I can not access variables outside the MyVistor (just like this).</p>
  
  <p>BTW, I found the question <a href="https://stackoverflow.com/questions/3163569/c0x-lambda-expression-does-look-same-as-javas-anonymous-inner-class">C++0x - lambda expression does look same as Java&#39;s anonymous inner class?</a> which comes close to the problem/question I have. Interesting is the answer from Johannes.</p>
  
  <p>Any hints or ideas would be welcome. Thanks for your thoughts and help!</p>