繁体   English   中英

默认情况下应调用移动构造函数

[英]Move constructor should be called by default

在以下情况下,我在 Integer class 中创建了移动 ctor,我希望在创建产品 object 时默认在右值引用上调用它,但我只调用构造函数。 Gcc - Ubuntu 18 上的 7.5.0

#include<iostream>
using namespace std;

class Integer 
{
    int *dInt = nullptr;
public: 
    Integer(int xInt)  {
        dInt = new int(xInt);
        cout<<"Integer Created"<<endl;
    } 
    Integer(const Integer &xObj)
    {
        cout<<"Copy called"<<endl;
        dInt = new int(xObj.mGetInt());
    }

    Integer(Integer &&xObj)
    {
        cout<<"Move called"<<endl;
        dInt = xObj.dInt;
        xObj.dInt = nullptr;
    }

    Integer& operator=(const Integer &xObj)
    {
        cout<<"Assignment operator called"<<endl;
        *dInt = xObj.mGetInt();
        return *this;
    }

    Integer& operator=(Integer &&xObj)
    {
        cout<<"Move Assignment operator called"<<endl;
        delete dInt;
        dInt = xObj.dInt;
        xObj.dInt = nullptr;
        return *this;   
    }
    ~Integer() 
    {
        cout<<"Integer destroyed"<<endl;
        delete dInt;
    }

    int mGetInt() const {return *dInt;}
};

class Product 
{
    Integer dId;
public: 
    Product(Integer &&xId)
    :dId(xId)
    {

    }
};
int main () 
{
    Product P(10); // Notice implicit conversion of 10 to Integer obj.
}

在上述情况下,如果我在产品 class ctor 中使用 dId(std::move(xId)) ,则调用移动,我期望它应该在右值引用上默认调用。 在以下情况下,我无法避免创建临时 object 的 Integer class,有什么好方法可以避免创建临时 ZA8CFDE6331BD4B862AC9.

    Product(const Integer &xId)
    :dId(xId)
    {

    }
    
    Product(10); // inside main

我上述问题的目的是建立我的理解,以便我可以更好地利用临时 object memory。

您需要std::move来“传播”右值引用。

主体内部如下 function:

void foo(int&& x);

…表达式x是一个左值int 不是int&&

引用并不真正“存在” ——即使它们由类型系统提供支持,它们也应该被视为别名(而不是单独的实体),因此在foo中使用x就像使用原始的、被引用的一样对待如您所知, int inside foo ... 并且这样做也会创建一个副本。


这将完成这项工作:

Product(Integer&& xId)
    : dId(std::move(xId))
{}

但是,我实际上鼓励您按价值取Integer

Product(Integer xId)
    : dId(std::move(xId))
{}

这样,您也可以使用相同的构造函数来传递左值Integer如果需要,将生成一个副本,而如果没有,则会自动发生移动(例如,通过传入文字,这将自动触发选择Integer的移动构造函数)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM