如何使用Move-emantics从类构造函数中重新分配资源?
为了使我能够阅读一个二进制文件,我已经完成了此类。我想不复制我试图使用移动语义的临时性。但是此代码产生“访问违规错误”。
#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;
}
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
临时销毁后,您将进入流。您需要将流存储在myClass对象中:
You are accessing the stream after the temporary has been destroyed. You need to store the stream in the myClass object:
问题是
input
inmyClass :: create()
的寿命。正如Jensenmckenzie所提到的那样,它将在该功能的末尾被破坏,因此将传递给返回的myClass
对象的 Reference 将是一个悬空的参考。 函数的唯一方法是存储一个具体保持
myClass :: create()
另一个std :: ifstream
要传递给构造函数,它必须作为R值参考:然后您可以编写
create> create()
函数这样的功能:请注意,您还可以写一个将路径名作为参数的构造函数:
然后您可以用
main()
而写下此构造函数:这根本不需要移动。
The problem is the lifetime of
input
inmyClass::create()
. As JensenMcKenzie mentioned, it will be destroyed at the end of that function, and therefore the reference passed to the returnedmyClass
object will be a dangling reference. The only way to keep themyClass::create()
function would be to store a concretestd::ifstream
object insidemyClass
:To allow another
std::ifstream
to be passed to the constructor, it has to be as an r-value reference:And then you can write the
create()
function like so:Note that you could also just have written a constructor that takes a pathname as an argument:
And then you could have written this in
main()
instead:This way no moves are necessary at all.