title: How do I use move-semantics to reallocate resources from class constructor?
body: <p>To enable me to read a binary file I have made this class. I would like to not copy a temporary for which I am trying to use move semantics. But this code produces &quot;access violation error&quot;.</p>
  <pre><code>#include &lt;fstream&gt;
  #include &lt;iostream&gt;
  
  class myClass
  {
  public:
      std::istream&amp; myStream;
      static myClass create(const char* path);
      myClass(myClass&amp;&amp;) noexcept = default;
      myClass(std::istream&amp; input);
      myClass() = default;
  };
  
  myClass myClass::create(const char* path)
  {
      std::ifstream input{ path, std::ios::binary | std::ios::in };
      if (!input.is_open()) throw std::runtime_error(&quot;Error - couldn't open file&quot;);
      return std::move(myClass(input));
  }
  
  myClass::myClass(std::istream&amp; input) :
      myStream(input)
  {
  
  }
  int main()
  {
      const char* path = R&quot;(file.bin)&quot;;
      myClass cr{myClass::create(path)};
  
      for (int i = 0; i &lt; 10; ++i)
          cr.myStream.seekg(i);
  
      return 0;
  }
  </code></pre>