title: adapter class for C++ interface with move constructor
body: <p>I am trying to write an <code>adapter</code> class for an interface class that accepts a) an implementation of the interface, which should be stack-allocated (so no new/delete handling should be required from the outside, the adapter itself may use new/delete) and b) a lambda function that will be called by a respective implementation of the interface.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;functional&gt;
  
  struct interface {
    virtual int hello() = 0;
  };
  
  struct implementation : public interface {
    virtual int hello() {
      std::cout &lt;&lt; "hello()\n";
      return 42;
    }
  };
  
  struct adapter {
    interface* obj;
  
    adapter(std::function&lt;int()&gt;&amp;&amp; func) {
      struct lambda : public interface {
        std::function&lt;int()&gt; func;
        lambda(std::function&lt;int()&gt; func_): func(func_) { }
        virtual int hello() {
          return this-&gt;func();
        }
      };
      this-&gt;obj = new lambda{func};
    }
  
    adapter(interface&amp;&amp; impl) {
      // TODO: pretty sure that's incorrect
      //       but can I somehow create a copy of "impl" on the heap?
      this-&gt;obj = &amp;impl;
    }
  };
  
  int main() {
    adapter a([]() { std::cout &lt;&lt; "hello from lambda\n"; return 99; });
    a.obj-&gt;hello();
  
  #if 0
    // ERROR
    adapter b(implementation());
    b.obj-&gt;hello();
  #endif
    return 0;
  }
  </code></pre>
  
  <p>This is the error I get when enabling the <code>adapter b</code> part.</p>
  
  <pre><code>prog.cpp: In function 'int main()':
  prog.cpp:39:4: error: request for member 'obj' in 'b', which is of non-class type 'adapter(implementation (*)())'
    b.obj-&gt;hello();
      ^
  </code></pre>
  
  <ol>
  <li>I don't understand the error at all, I would much appreciate an explanation</li>
  <li>How can I actually correctly implement the <code>adapter(interface&amp;&amp;)</code> constructor? I will probably need to create a copy of the object on the heap, otherwise it won't be persistent after the <code>adapater</code> constructor</li>
  </ol>
  
  <p>Tested on ideone: <a href="http://ideone.com/Gz3ICk" rel="nofollow">http://ideone.com/Gz3ICk</a> with C++14 (gcc-5.1)</p>
  
  <p>PS: Yes the <code>adapter</code> class lacks a destructor that should delete <code>obj</code> created from the lambda constructor</p>