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 "access violation error".
#include <fstream>
#include <iostream>
class myClass
{
public:
std::istream& myStream;
static myClass create(const char* path);
myClass(myClass&&) noexcept = default;
myClass(std::istream& 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("Error - couldn't open file");
return std::move(myClass(input));
}
myClass::myClass(std::istream& input) :
myStream(input)
{
}
int main()
{
const char* path = R"(file.bin)";
myClass cr{myClass::create(path)};
for (int i = 0; i < 10; ++i)
cr.myStream.seekg(i);
return 0;
}
Aucun commentaire:
Enregistrer un commentaire