小编Mil*_*low的帖子

移动构造函数c ++

什么是移动构造函数的正确方法?

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值是否有用?

c++ constructor

1
推荐指数
1
解决办法
97
查看次数

fwrite 从字符串中写入二进制数据

我想创建一个文件并将“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

您发现这段代码有问题吗?

c++ fwrite

1
推荐指数
1
解决办法
2876
查看次数

标签 统计

c++ ×2

constructor ×1

fwrite ×1