灵活而奇特的C++语言特性——引用(下)

       学习了博主的《漫谈继承技术》系列博文之后,相信大家都有所收获吧!这次博主将和大家一起探讨 《灵活而奇特的C++语言特性》 ,主要包括引用、常量(const)、常量表达式(constexpr)、静态(static)、外部(expert)、类型定义(typedef)、类型别名(aliases)、类型转换、作用域解析、统一初始化、显示转换运算符、特性(attribute)、用户自定义文本、头文件、可变长度参数列表和预处理器宏。尽管这个知识清单显得有点凌乱,但是这些话题都是博主经过精心挑选,是容易混淆的语言特性。本篇我们来继续学习一下引用,增进大家对《灵活而奇特的C++语言特性》的理解。

       经过上一篇博文的学习,相信大家对引用已经掌握的差不多了吧!接下来我们一起来继续探讨引用的其他特性,本篇主要围绕右值引用和移动语义展开。关于移动语义的讲解,博主封装了一个异常安全的MyString类,简单的模拟了一下string的封装,希望对大家对《灵活而奇特的C++语言特性》的学习和掌握有一点帮助。

      了解过C++11新特性的小伙伴们一定听说过移动语义,而移动语义要用到一种新的语法,就是右值引用。在C++中,左值是可以获取其地址的一个量,例如一个有名称的变量。由于经常出现在赋值语句的左边,因此将其称为左值。回顾左值的概念后,相信大家已经猜到什么是右值了。对,所有不是左值的量都是右值,例如常量值、临时对象或临时值。而右值引用就是一个对右值的引用。上面的描述可能对于初学者显得有点抽象,咱们来举个栗子吧。

#include<iostream>

usingnamespacestd;

 

//参数为左值引用

void increace(int&nValue)

{

    ++nValue;

    cout<<"void increace(int &nValue)"<< endl;

}

 

//参数为右值引用

void increace(int&&nValue)

{

    ++nValue;

    cout<<"void increace(int &&nValue)"<< endl;

}

 

int main(intargc,char**argv)

{

    intnValue = 10;

    constintnCValue = 20;

 

    //调用参数为左值引用的increace函数

    increace(nValue);

    //调用参数为右值引用的increace函数

    increace(30);

   

    //常量值可以作为右值引用的参数,然而常量不可以

    //increace(nCValue);    //error

 

    cout<<"nValue: "<< nValue << endl;

    cout<<"nCValue: "<< nCValue << endl;

   

    return0;

}

 

程序运行结果:


         知道了右值引用,那移动语义又是怎么回事呢?咱们来写一个自己的MyString类来体验一下吧。

#define_CRT_SECURE_NO_WARNINGS

#include<iostream>

#include<vector>

usingnamespacestd;

 

//封装MyString类实现string类的功能

classMyString

{

public:

    MyString(constchar*cPStr =nullptr);

    virtual~MyString();

 

    MyString(constMyString &rhs);

    MyString(MyString &&rhs);

    MyString& operator = (constMyString &rhs);

    MyString& operator = (MyString&&rhs);

    MyString operator + (const MyString &rhs);

    MyString& operator += (constMyString &rhs);

 

private:

    char*m_cPStr;

};

 

MyString::MyString(constchar*cPStr/*nullptr*/)

{

    cout<<"call: MyString::MyString(char *cPStr/*nullptr*/)" << endl;

 

    if(nullptr== cPStr)

    {

        m_cPStr=new char[1];

        *m_cPStr='\0';

    }

    else

    {

        //求参数对象数据成员字符串长度(加1是为了保存'\0'

        constintsize = strlen(cPStr) + 1;

        m_cPStr=new char[size];

        //内存拷贝

        memcpy(m_cPStr,cPStr,size);

    }

}

 

MyString::~MyString()

{

    cout<<"call: MyString::~MyString()"<< endl;

    if(nullptr!= m_cPStr)

    {

        delete[]m_cPStr;

        m_cPStr=nullptr;

    }

}

 

MyString::MyString(constMyString&rhs)

{

    cout<<"call: MyString::MyString(const MyString&rhs)" << endl;

 

    //求参数对象数据成员字符串长度(加1是为了保存'\0'

    constintsize = strlen(rhs.m_cPStr) + 1;

    m_cPStr =newchar[size];

    //内存拷贝

    memcpy(m_cPStr,rhs.m_cPStr,size);

}

 

MyString&MyString::operator= (constMyString&rhs)

{

    cout<<"call: MyString& MyString::operator = (constMyString& rhs)" << endl;

   

    //当自己给自己赋值时,直接返回当前对象

    if(this== &rhs)

    {

        return*this;

    }

 

    //此处调用拷贝构造函数创建局部对象,当函数结束时该对象被销毁

    MyString str(rhs);

 

    //利用局部对象在离开语句块会被销毁的特性,编写异常安全的代码

    char*cTemp = m_cPStr;

    m_cPStr =str.m_cPStr;

    str.m_cPStr= cTemp;

 

    return*this;

}

 

MyString::MyString(MyString &&rhs)

{

    cout<<"call: MyString::MyString(MyString&&rhs)" << endl;

 

    //将右值对象数据成员在堆上的内存赋值给当前对象的数据成员

    m_cPStr =rhs.m_cPStr;

    //将右值对象数据成员赋为nullptr

    rhs.m_cPStr= nullptr;

}

 

MyString&MyString::operator= (MyString&&rhs)

{

    cout<<"call: MyString& MyString::operator =(MyString&& rhs)" << endl;

   

    //当自己给自己赋值时,直接返回当前对象

    if(this== &rhs)

    {

        return*this;

    }

 

    //释放左值对象在堆上的内存

    if(nullptr!= m_cPStr)

    {

        delete[]m_cPStr;

    }

 

    //将右值对象数据成员在堆上的内存赋值给当前对象的数据成员

    m_cPStr =rhs.m_cPStr;

 

    //将右值对象数据成员赋为nullptr

    rhs.m_cPStr= nullptr;

 

    return*this;

}

 

MyStringMyString::operator+ (constMyString&rhs)

{

    constintsize = strlen(m_cPStr) + strlen(rhs.m_cPStr)+ 1;

    char*cPstr = new char[size];

    strcpy(cPstr,m_cPStr);

    strcat(cPstr,rhs.m_cPStr);

 

    MyString str(cPstr);

    delete[]cPstr;

    cPstr =nullptr;

 

    returnstr;

}

 

MyString&MyString::operator+= (constMyString&rhs)

{

    constintsize = strlen(m_cPStr) + strlen(rhs.m_cPStr)+ 1;

    char*cPstr = new char[size];

    strcpy(cPstr,m_cPStr);

    strcat(cPstr,rhs.m_cPStr);

 

    MyString str(cPstr);

    delete[]cPstr;

    cPstr =nullptr;

 

    char*cPTemp = str.m_cPStr;

    str.m_cPStr= m_cPStr;

    m_cPStr =cPTemp;

 

    return*this;

}

 

int main(intargc,char**argv)

{

    //创建矢量

    vector<MyString>vArray;

 

    //调用构造函数创建对象

    MyString str1("string1");

 

    //调用拷贝构造函数创建对象

    MyString str2(str1);

 

    //调用构造函数创建临时对象(编译器会做优化,将临时对象直接作为str3

    MyString str3 = MyString("string2");

 

    //使用std::move()将左值转换为右值,此处调用移动构造函数创建对象

    MyString str4(std::move(str1));

 

    //调用复制赋值运算符

    str2 =str3;

 

    //先调用构造函数创建临时对象,再调用移动赋值运算符

    str3 =MyString("string3");

 

    vArray.reserve(2);

    vArray.push_back(str2);

    vArray.push_back(str3);

 

    //先调用operator+,再调用operator=

    str3 =str2 +"string4";

 

    //调用operator+=

    str3 +="string5";

 

    return0;

}

 

程序运行结果:


下面我们来逐条分析一下上面的运行结果:

1)        call: MyString::MyString(char*cPStr /*nullptr*/)

MyStringstr1("string1");  //调用构造函数创建对象str1

2)        call: MyString::MyString(constMyString& rhs)

MyString str2(str1);  //调用拷贝构造函数创建对象str2

3)        call: MyString::MyString(char*cPStr /*nullptr*/)

MyString str3 =MyString("string2");  //调用构造函数创建对象str3(由于编译器做了优化,将str3和“=”右边的临时对象作为一个对象来创建)

4)        call:MyString::MyString(MyString &&rhs)

MyString str4(std::move(str1));   //std::move(str1)将str1由左值转换为右值,调用移动构造函数创建对象str4

5)        call: MyString&MyString::operator = (const MyString& rhs)

str2= str3;  //调用复制赋值运算符

6)        call: MyString::MyString(constMyString& rhs)

str2= str3;  //在复制赋值运算符中“MyString str(rhs);”调用拷贝构造函数创建局部对象

7)        call: MyString::~MyString()

str2= str3;  //复制赋值运算符调用结束后,局部对象被销毁,调用析构函数

8)        call: MyString::MyString(char*cPStr /*nullptr*/)

str3= MyString("string3");  //创建“=”右边的临时对象,调用构造函数

9)        call: MyString&MyString::operator = (MyString&& rhs)

str3= MyString("string3");  //由于临时对象是右值,调用移动赋值运算符

10)    call: MyString::~MyString()

str3= MyString("string3");  //语句结束后,“=”右边的临时对象被销毁,调用析构函数

11)    call: MyString::MyString(constMyString& rhs)

vArray.push_back(str2);  //调用拷贝构造函数将str2拷贝到矢量容器中

12)    call: MyString::MyString(constMyString& rhs)

vArray.push_back(str3);  //调用拷贝构造函数将str3拷贝到矢量容器中

13)    call: MyString::MyString(char*cPStr /*nullptr*/)

str3= str2 + "string4";  //调用构造函数创建“+”右边的临时对象

14)    call: MyStringMyString::operator + (const MyString &rhs)

str3= str2 + "string4";  //调用operator +函数

15)    call: MyString::MyString(char*cPStr /*nullptr*/)

str3= str2 + "string4";  //由于在operator + 函数中“MyString str(cPstr);”创建了临时对象,调用构造函数

16)    call:MyString::MyString(MyString &&rhs)

str3= str2 + "string4";  //operator + 函数返回,调用移动构造函数创建返回对象

17)    call: MyString::~MyString()

str3= str2 + "string4";   //operator + 函数返回后,函数中创建的临时对象被销毁,调用析构函数

18)    call: MyString&MyString::operator = (MyString&& rhs)

str3= str2 + "string4";  //由于“str2+ "string4"”表达式是右值,调用移动赋值运算符

19)    call: MyString::~MyString()

str3= str2 + "string4";  //operator + 函数返回的临时对象被销毁,调用析构函数

20)    call: MyString::~MyString()

str3= str2 + "string4";  //“+”右边的临时对象被销毁,调用析构函数

21)    call: MyString::MyString(char*cPStr /*nullptr*/)

str3+= "string5";  //创建“+=”右边的临时对象,调用构造函数

22)    call: MyStringMyString::operator += (const MyString &rhs)

str3+= "string5";  //调用operator +=函数

23)    call: MyString::MyString(char*cPStr /*nullptr*/)

str3+= "string5";  //operator +=函数中“MyString str(cPstr);”调用构造函数,创建局部对象

24)    call: MyString::~MyString()

str3+= "string5";  //operator +=函数返回后,“MyString str(cPstr);”创建的局部对象被销毁,调用析构函数

25)    call: MyString::~MyString()

str3+= "string5"; “+=”右边的临时对象被销毁,调用析构函数

26)    call: MyString::~MyString()

vArray中创建的第二个对象被销毁,调用析构函数

27)    call: MyString::~MyString()

vArray中创建的地一个对象被销毁,调用析构函数

28)    call: MyString::~MyString()

str4被销毁,调用析构函数

29)    call: MyString::~MyString()

str3被销毁,调用析构函数

30)    call: MyString::~MyString()

str2被销毁,调用析构函数

31)    call: MyString::~MyString()

str1被销毁,调用析构函数

        

       上面的代码中有用到std::move()方法,它可以将左值转换为右值,这是我们想用左值调用移动函数的一种方式。移动语义是通过右值引用来实现的。移动构造函数和移动赋值运算符基本上只对成员变量进行表层复制,然后转换已分配内存的所有权,从而阻止悬挂指针和内存泄露。

       如果你现在头脑非常清晰,说明你已经掌握了此部分知识,当然你对对象的创建和销毁顺序也掌握的很透彻。如果你还是一脸懵逼,那么就请你先好好复习一下对象的创建和销毁顺序,再回来吧这篇博文仔细的阅览一遍,相信你会有不一样的感受。

       如果想了解更多关于C++语言特性相关的知识,请关注博主《灵活而奇特的C++语言特性》系列博文,相信你能够在那里寻找到更多有助你快速成长和加深你对C++语言特性相关的知识和一些特性的理解和掌握。当然,如果你想了解关于继承方面的技术,请关注博主《漫谈继承技术》系列博文。


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值