草庐IT

c++ - 当 move 和复制构造函数都存在时,将调用哪一个?

下面是A类,它充满了不同类型的构造函数。如果我注释move构造函数,则复制构造函数被调用两次:一次是通过值传递一个对象到函数fun,另一次是从同一个函数返回。代码片段A类{intx;public:A(){cout};intmain(){Aa;Ab;Ac;c=a.fun(b);}输出:DefaultConstructorDefaultConstructorDefaultConstructorCopyConstructorMoveConstructor但是,如果存在move构造函数,则调用它而不是复制构造函数。任何人都可以用一个很好的例子来详细说明这一点,这样我就会清楚这个概念。非常感谢您

c++ - 当 move 和复制构造函数都存在时,将调用哪一个?

下面是A类,它充满了不同类型的构造函数。如果我注释move构造函数,则复制构造函数被调用两次:一次是通过值传递一个对象到函数fun,另一次是从同一个函数返回。代码片段A类{intx;public:A(){cout};intmain(){Aa;Ab;Ac;c=a.fun(b);}输出:DefaultConstructorDefaultConstructorDefaultConstructorCopyConstructorMoveConstructor但是,如果存在move构造函数,则调用它而不是复制构造函数。任何人都可以用一个很好的例子来详细说明这一点,这样我就会清楚这个概念。非常感谢您

c++ - move 赋值运算符和 move 构造函数之间的区别?

一段时间以来,这一直让我感到困惑。到目前为止,我还没有找到满意的答案。问题很简单。什么时候调用moveassignmentoperator,什么时候调用moveconstructoroperator?cppreference.com上的代码示例产生了以下有趣的结果:Themoveassignmentoperator:a2=std::move(a1);//move-assignmentfromxvalueThemoveconstructor:Aa2=std::move(a1);//move-constructfromxvalue那么它与哪个实现有关?如果是这样,如果两者都实现,则执行哪个

c++ - move 赋值运算符和 move 构造函数之间的区别?

一段时间以来,这一直让我感到困惑。到目前为止,我还没有找到满意的答案。问题很简单。什么时候调用moveassignmentoperator,什么时候调用moveconstructoroperator?cppreference.com上的代码示例产生了以下有趣的结果:Themoveassignmentoperator:a2=std::move(a1);//move-assignmentfromxvalueThemoveconstructor:Aa2=std::move(a1);//move-constructfromxvalue那么它与哪个实现有关?如果是这样,如果两者都实现,则执行哪个

c++ - 在 move 构造函数中窃取

在一个玩具类的move构造函数的实现过程中,我注意到一个模式:array2D(array2D&&that){data_=that.data_;that.data_=0;height_=that.height_;that.height_=0;width_=that.width_;that.width_=0;size_=that.size_;that.size_=0;}模式显然是:member=that.member;that.member=0;所以我写了一个预处理器宏来让窃取变得不那么冗长和容易出错:#defineSTEAL(member)member=that.member;that.

c++ - 在 move 构造函数中窃取

在一个玩具类的move构造函数的实现过程中,我注意到一个模式:array2D(array2D&&that){data_=that.data_;that.data_=0;height_=that.height_;that.height_=0;width_=that.width_;that.width_=0;size_=that.size_;that.size_=0;}模式显然是:member=that.member;that.member=0;所以我写了一个预处理器宏来让窃取变得不那么冗长和容易出错:#defineSTEAL(member)member=that.member;that.

c++ - 使类不可复制*和*不可 move

在C++11之前,我可以使用它来使类不可复制:private:MyClass(constMyClass&);MyClass&operator=(constMyClass&);使用C++11,我可以这样做:MyClass(constMyClass&)=delete;MyClass&operator=(constMyClass&)=delete;当使用带有已删除拷贝和赋值的类时,是否有可能生成默认的move运算符?而且这个类并没有完全复制,而是move了(有点相似)?那么,我是否必须这样做以防止默认move构造和分配:MyClass(MyClass&&)=delete;MyClass&op

c++ - 使类不可复制*和*不可 move

在C++11之前,我可以使用它来使类不可复制:private:MyClass(constMyClass&);MyClass&operator=(constMyClass&);使用C++11,我可以这样做:MyClass(constMyClass&)=delete;MyClass&operator=(constMyClass&)=delete;当使用带有已删除拷贝和赋值的类时,是否有可能生成默认的move运算符?而且这个类并没有完全复制,而是move了(有点相似)?那么,我是否必须这样做以防止默认move构造和分配:MyClass(MyClass&&)=delete;MyClass&op

C++17 表达式求值顺序和 std::move

今天在重构一些代码以更改指向std::unique_ptr的原始指针时,我遇到了由于orderofevaluation导致的段错误错误。旧代码做了如下的事情:voidadd(conststd::string&name,Foo*f){_foo_map[name]=f;}voidprocess(Foo*f){add(f->name,f);}第一次天真地重构代码以使用std::unique_ptr:voidadd(conststd::string&name,std::unique_ptrf){_foo_map[name]=std::move(f);}voidprocess(std::uniq

C++17 表达式求值顺序和 std::move

今天在重构一些代码以更改指向std::unique_ptr的原始指针时,我遇到了由于orderofevaluation导致的段错误错误。旧代码做了如下的事情:voidadd(conststd::string&name,Foo*f){_foo_map[name]=f;}voidprocess(Foo*f){add(f->name,f);}第一次天真地重构代码以使用std::unique_ptr:voidadd(conststd::string&name,std::unique_ptrf){_foo_map[name]=std::move(f);}voidprocess(std::uniq