什么是移动构造函数的正确方法?
class A{
...some stuff...
private:
int i;
std::string str;
};
A::A(A &&a)
{
*this = std::move(a);
};
Run Code Online (Sandbox Code Playgroud)
要么
A::A(A &&a)
{
this->str = std::move(a.str);
};
Run Code Online (Sandbox Code Playgroud)
在第二种情况下,std :: move()int值是否有用?
我想创建一个文件并将“A”写入其中(ascii 为 65 == 01000001)。奇怪的是,无论 的值是多少std::string binary,其中总是写有字母myfile.txtP。
std::string binary = "01000001";
std::string file = "myfile.txt";
FILE* f;
f = fopen(file.c_str(), "wb");
fwrite(&binary, 1, 1, f);
fclose(f);
Run Code Online (Sandbox Code Playgroud)
执行此代码后,我使用命令读取二进制数据xxd -b myfile,得到以下结果:
00000000:01010000
您发现这段代码有问题吗?