设为首页 加入收藏

TOP

8.5.1 避免不必要的复制操作(1)
2013-10-07 16:07:31 来源: 作者: 【 】 浏览:53
Tags:8.5.1 避免 必要 复制 操作

8.5.1  避免不必要的复制操作(1)

通过修改Ex8_05.cpp的CMessage类,我们来看看如何避免不必要的复制操作。下面是CMessage类实现相加运算符之后的版本。

  1. class CMessage  
  2. {  
  3. private:  
  4. char* pmessage;                       // Pointer to object text string  
  5. public:  
  6. // Function to display a message  
  7. void ShowIt() const  
  8. {  
  9. cout << endl << pmessage;  
  10. }  
  11. // Overloaded addition operator  
  12. CMessage operator+(const CMessage& aMess) const  
  13. {  
  14. cout << "Add operator function called." << endl;  
  15. size_t len = strlen(pmessage) + strlen(aMess.pmessage) + 1;  
  16. CMessage message;  
  17. message.pmessage = new char[len];  
  18. strcpy_s(message.pmessage, len, pmessage);  
  19. strcat_s(message.pmessage, len, aMess.pmessage);  
  20. return message;  
  21. }  
  22. // Overloaded assignment operator for CMessage objects  
  23. CMessage& operator=(const CMessage& aMess)  
  24. {  
  25. cout << "Assignment operator function called." << endl;  
  26. if(this != &aMess) // Check addresses are not equal  
  27. {  
  28. // Release memory for 1st operand  
  29. delete[] pmessage;  
  30. pmessage = new char[strlen(aMess.pmessage) + 1];  
  31. // Copy 2nd operand string to 1st  
  32. strcpy_s(this->pmessage, strlen(aMess.pmessage)+1, aMess.pmessage);  
  33. }  
  34. return *this;                                                                                   // Return a reference to 1st operand  
  35. }  
  36. // Constructor definition  
  37. CMessage(const char* text = "Default message")  
  38. {  
  39. cout << "Constructor called." << endl;  
  40. pmessage = new char[strlen(text) + 1];                          // Allocate space for text  
  41. strcpy_s(pmessage, strlen(text)+1, text);               // Copy text to new memory  
  42. }  
  43. // Copy constructor definition  
  44. CMessage(const CMessage& aMess)  
  45. {  
  46. cout << "Copy constructor called." << endl;  
  47. size_t len = strlen(aMess.pmessage)+1;  
  48. pmessage = new char[len];  
  49. strcpy_s(pmessage, len, aMess.pmessage);  
  50. }  
  51. // Destructor to free memory allocated by new  
  52. ~CMessage()  
  53. {  
  54. cout << "Destructor called."                    // Just to track what happens  
  55. << endl;  
  56. delete[] pmessage;                                                      // Free memory assigned to pointer  
  57. }  
  58. };  

对Ex8_05.cpp版本所做的修改采用突出显示。现在从构造函数和赋值运算符函数的输出,来跟踪何时调用它们。此类中还有一个复制构造函数和一个operator+()函数。operator+()函数用来将两个CMessage对象加起来。我们可以添加将CMessage对象用字符串字面量连接起来的版本,但根据目前的用途,没必要这么做。通过CMessage对象上的一些简单操作,来看看复制时都发生了些什么。

试一试:跟踪对象的复制操作

下面的代码用来练习CMessage类:

  1. // Ex8_07.cpp  
  2. // How many copy operations  
  3. #include <iostream> 
  4. #include <cstring> 
  5. using std::cout;  
  6. using std::endl;  
  7. // Insert CMessage class definition here...  
  8. int main()  
  9. {  
  10. CMessage motto1("The devil takes care of his own. ");  
  11. CMessage motto2("If you sup with the devil use a long spoon.\n");  
  12. CMessage motto3;  
  13. cout << " Executing: motto3 = motto1 + motto2 " << endl;;  
  14. motto3 = motto1 + motto2;  
  15. cout << " Done!! " << endl << endl;  
  16. cout << " Executing: motto3motto3 = motto3 + motto1 + motto2 " << endl;  
  17. motto3motto3 = motto3 + motto1 + motto2;  
  18. cout << " Done!! " << endl << endl;  
  19. cout << "motto3 contains - ";  
  20. motto3.ShowIt();  
  21. cout << endl;  
  22. return 0;  
  23. }  

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇8.5 对象复制问题 下一篇8.5.1 避免不必要的复制操作(2)

评论

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

·用 C 语言或者限制使 (2025-12-25 08:50:05)
·C++构造shared_ptr为 (2025-12-25 08:50:01)
·既然引用计数在做 GC (2025-12-25 08:49:59)
·Java 编程和 c 语言 (2025-12-25 08:19:48)
·. net内存管理宝典这 (2025-12-25 08:19:46)