0
点赞
收藏
分享

微信扫一扫

C++ 11特性深入学习

有点d伤 2022-01-17 阅读 58

若一个类重载  =  符号

class_name& operator=(const class_name& other)

通过函数返回一个临时的class来定义一个  已声明的  变量时 

class_name c = class_name(arg);

会进行较多的工作:先用arg的对应构造函数生成一个临时var, 然后=重载函数将var作为参数才能生成c.

而通过右值引用重载 =

class_name& operator=(class_name&& other)
{
    sawp(this->data, other.data);
}

与上相同的方式定义变量时,直接将other(临时变量)的内部空间变成this的,仅进行一次构造和析构。

 简单地说,减少右值初始化左值的开销。

举报

相关推荐

0 条评论