右值引用和移动语义

什么是右值什么是左值,已经很多类似的文章介绍了。我这里直接说重点,为什么我们需要转移语义

class Foo{
public:
    Foo():a(0){

    }
    Foo(int a_):a(a_){
        printf("constructor\n");
    }
    Foo(const Foo& rhs){
        printf("const copy constructor\n");
        this->a = rhs.a;
    }

    Foo& operator=(const Foo& rhs){
        printf("assign constructor\n");
        this->a = rhs.a;
        return *this;
    }
private:
    int a;
};

我们考虑上面这个类,我们现在想进行如下操作

int main() {
    std::vector<Foo> vec;
    Foo foo;
    foo = Foo(10);
    vec.push_back(foo);
    return 0;
}

先是调用了构造函数,然后赋值构造函数,接着push_back的时候调用了拷贝构造函数。
很显然这里foo只是个临时变量,而我们却为这个左值付出了构造然后销毁的代价。Foo(10)是一个右值,我们本可以直接用这个来进行操作的不是吗?
我们给上面那个类添加两个函数

    Foo(const Foo&& rhs){
        printf("move constructor\n");
        this->a = rhs.a;
    }
    Foo& operator=(const Foo&& rhs){
        printf("move assign constructor\n");
        this->a = rhs.a;
    }

接受右值作为参数,这样一来,我们在用左值和右值进行初始化构造的时候,会调用不同的函数。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值