小编Mił*_*zyn的帖子

构造函数 - 复制常量引用 VS 移动复制值

我找不到答案,所以我在这里提问。这有什么区别:

class Foo
{
public:
    Foo(std::string string)
        : m_String(std::move(string)) {}

private:
    std::string m_String;
}
Run Code Online (Sandbox Code Playgroud)

然后:

class Bar
{
public:
    Bar(const std::string& string)
        : m_String(string) {}

private:
    std::string m_String;
}
Run Code Online (Sandbox Code Playgroud)

我知道使用构造函数 fromBar可以准确地从值复制数据,但我真的不知道调用Foo构造函数时到底会发生什么。使用Fooor的构造函数有区别吗Bar

c++ constructor

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

标签 统计

c++ ×1

constructor ×1