设为首页 加入收藏

TOP

8.5.3 命名的对象是lvalue(1)
2013-10-07 16:09:24 来源: 作者: 【 】 浏览:69
Tags:8.5.3 命名 对象 lvalue

8.5.3  命名的对象是lvalue(1)

当调用CMessage类中具有rvalue引用形参的赋值运算符函数时,我们肯定知道实参(即右操作数)是一个rvalue,因此,它是一个临时对象,我们可以偷用它的内存。但是,在此运算符函数的函数体内,形参aMess是一个lvalue。这是因为任何表达式,如果是一个命名的变量,则它是一个lvalue。这可能会重新导致效率低下,我们会通过下面这个示例来演示说明这一点,该示例使用CMessage类的修改版本。

  1. class CMessage  
  2. {  
  3. private:  
  4. CText text;                      // Object text string  
  5. public:  
  6. // Function to display a message  
  7. void ShowIt() const  
  8. {  
  9. text.ShowIt();  
  10. }  
  11. // Overloaded addition operator  
  12. CMessage operator+(const CMessage& aMess) const  
  13. {  
  14. cout << "CMessage add operator function called." << endl;  
  15. CMessage message;  
  16. message.text = text + aMess.text;  
  17. return message;  
  18. }  
  19. // Copy assignment operator for CMessage objects  
  20. CMessage& operator=(const CMessage& aMess)  
  21. {  
  22. cout << "CMessage copy assignment operator function called." << endl;  
  23. if(this == &aMess)                      // Check addresses, if equal  
  24. {  
  25. text = aMess.text;  
  26. }  
  27. return *this;                           // Return a reference to 1st operand  
  28. }  
  29. // Move assignment operator for CMessage objects  
  30. CMessage& operator=(CMessage&& aMess)  
  31. {  
  32. cout << "CMessage move assignment operator function called." << endl;  
  33. text = aMess.text;  
  34. return *this;                           // Return a reference to 1st operand  
  35. }  
  36. // Constructor definition  
  37. CMessage(const char* str = "Default message")  
  38. {  
  39. cout << "CMessage constructor called." << endl;  
  40. text = CText(str);  
  41. }  
  42. // Copy constructor definition  
  43. CMessage(const CMessage& aMess)  
  44. {  
  45. cout << "CMessage copy constructor called." << endl;  
  46. text = aMess.text;  
  47. }  
  48. // Move constructor definition  
  49. CMessage(CMessage&& aMess)  
  50. {  
  51. cout << "CMessage move constructor called." << endl;  
  52. text = aMess.text;  
  53. }  
  54. };  

消息文本现在存储为CText类型的对象,CMessage类的成员函数也进行了相应的修改。需要注意的是,CMessage类具有rvalue引用版本的复制构造函数和赋值运算符,因此在可能的情况下,它应该是移动而不是创建新对象。下面是CText类的定义:

  1. class CText  
  2. {  
  3. private:  
  4. char* pText;  
  5. public:  
  6. // Function to display text  
  7. void ShowIt() const  
  8. {  
  9. cout << pText << endl;  
  10. }  
  11. // Constructor  
  12. CText(const char* pStr="No text")  
  13. {  
  14. cout << "CText constructor called." << endl;  
  15. size_t len(strlen(pStr)+1);  
  16. pText = new char[len];                              // Allocate space for text  
  17. strcpy_s(pText, len, pStr);                        // Copy text to new memory  
  18. }  
  19. // Copy constructor definition  
  20. CText(const CText& txt)  
  21. {  
  22. cout << "CText copy constructor called." << endl;  
  23. size_t len(strlen(txt.pText)+1);  
  24. pText = new char[len];  
  25. strcpy_s(pText, len, txt.pText);  
  26. }  
  27. // Move constructor definition  
  28. CText(CText&& txt)  
  29. {  
  30. cout << "CText move constructor called." << endl;  
  31. pText = txt.pText;  
  32. txt.pText = nullptr;  
  33. }  
  34. // Destructor to free memory allocated by new  
  35. ~CText()  
  36. {  
  37. cout << "CText destructor called." << endl;     // Just to track what happens  
  38. delete[] pText;                                                                                                         // Free memory  
  39. }  
  40. // Assignment operator for CText objects  
  41. CText& operator=(const CText& txt)  
  42. {  
  43. cout << "CText assignment operator function called." << endl;  
  44. if(this != &txt)                                                                                    // Check addresses not equal  
  45. {  
  46. delete[] pText;                                                                             // Release memory for 1st operand  
  47. size_t len(strlen(txt.pText)+1);  
  48. pText = new char[len];  
  49. // Copy 2nd operand string to 1st  
  50. strcpy_s(this->pText, len, txt.pText);  
  51. }  
  52. return *this;                                 // Return a reference to 1st operand  
  53. }  
  54. // Move assignment operator for CText objects  
  55. CText& operator=(CText&& txt)  
  56. {  
  57. cout << "CText move assignment operator function called." << endl;  
  58. delete[] pText;                                 // Release memory for 1st operand  
  59. pText = txt.pText;  
  60. txt.pText = nullptr;  
  61. return *this;                                   // Return a reference to 1st operand  
  62. }  
  63. // Overloaded addition operator  
  64. CText operator+(const CText& txt) const  
  65. {  
  66. cout << "CText add operator function called." << endl;  
  67. size_t len(strlen(pText) + strlen(txt.pText) + 1);  
  68. CText aText;  
  69. aText.pText = new char[len];  
  70. strcpy_s(aText.pText, len, pText);  
  71. strcat_s(aText.pText, len, txt.pText);  
  72. return aText;  
  73. }  
  74. };  

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇8.5.2 应用rvalue引用形参 下一篇8.5.3 命名的对象是lvalue(2)

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·python数据分析岗的 (2025-12-25 10:02:21)
·python做数据分析需 (2025-12-25 10:02:19)
·成为一个优秀的pytho (2025-12-25 10:02:16)
·Java后端面试实习自 (2025-12-25 09:24:21)
·Java LTS版本有哪些 (2025-12-25 09:24:18)