加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

默认的析构函数用法

(2010-10-20 20:33:36)
标签:

it

分类: C加加

 默认的析构函数

我们迄今使用过的所有对象都是被类的默认析构函数自动销毁的。如果我们不自行定义析构函数,编译器就总是生成默认的析构函数。默认的析构函数不能删除new运算符在自由存储器中分配的对象或对象成员。如果类成员占用的空间是在构造函数中动态分配的,我们就必须自定义析构函数,然后显式使用delete运算符来释放构造函数使用new运算符分配的内存,就像销毁普通变量一样。我们需要在编写析构函数方面实践一下,因此下面就来试一试。

试一试:简单的析构函数

为了解何时调用类的析构函数,我们可以在CBox类中添加析构函数。下面是本示例的代码,其中包括的CBox类有一个析构函数:

  1. // Ex8_01.cpp  
  2. // Class with an explicit destructor  
  3. #include <iostream> 
  4. using std::cout;  
  5. using std::endl;  
  6.  
  7. class CBox                                          
    // Class definition at global scope  
  8. {  
  9. public:  
  10. // Destructor definition  
  11. ~CBox()  
  12. {  
  13. cout << "Destructor called." << endl;  
  14. }  
  15.  
  16. // Constructor definition  
  17. CBox(double lv 1.0, double wv 1.0, double hv 1.0):  
  18. m_Length(lv), m_Width(wv), m_Height(hv)  
  19. {  
  20. cout << endl << "Constructor called.";  
  21. }  
  22.  
  23. // Function to calculate the volume of box  
  24. double Volume() const  
  25. {  
  26. return m_Length*m_Width*m_Height;  
  27. }  
  28.  
  29. // Function to compare two boxes which returns true  
  30. // if the first is greater that the second, and false otherwise  
  31. int compare(CBox* pBox) const  
  32. {  
  33. return this->Volume() > pBox->Volume();  
  34. }  
  35. private:  
  36. double m_Length;                                       
    // Length of box in inches  
  37. double m_Width;                                     
    // Width of box in inches  
  38. double m_Height;                                      
    // Height of box in inches  
  39. };  
  40.  
  41. // Function to demonstrate the CBox class destructor in action  
  42. int main()  
  43. {  
  44. CBox boxes[5];                                               
    // Array of CBox objects declared  
  45. CBox cigar(8.0, 5.0, 1.0);              // Declare cigar box  
  46. CBox match(2.2, 1.1, 0.5);              // Declare match box  
  47. CBox* pB1 &cigar;                                         
    // Initialize pointer to cigar object address  
  48. CBox* pB2 0                                         
    // Pointer to CBox initialized to null  
  49.  
  50. cout << endl 
  51. << "Volume of cigar is "  
  52. << pB1->Volume();                             
    // Volume of obj. pointed to  
  53.  
  54. pB2 boxes                                                  
    // Set to address of array  
  55. boxes[2] match;                                     
    // Set 3rd element to match  
  56. cout << endl 
  57. << "Volume of boxes[2] is "  
  58. << (pB2 2)->Volume();         // Now access thru pointer  
  59.  
  60. cout << endl;  
  61. return 0;  

示例说明

CBox类的析构函数仅仅显示一条宣称"析构函数被调用"的消息。该示例的输出如下:

  1. Constructor called.  
  2. Constructor called.  
  3. Constructor called.  
  4. Constructor called.  
  5. Constructor called.  
  6. Constructor called.  
  7. Constructor called.  
  8. Volume of cigar is 40  
  9. Volume of boxes[2] is 1.21  
  10. Destructor called.  
  11. Destructor called.  
  12. Destructor called.  
  13. Destructor called.  
  14. Destructor called.  
  15. Destructor called.  
  16. Destructor called. 
在程序结束时,每个对象都需要调用一次析构函数。每出现一次构造函数的调用,就有一次匹配的析构函数调用。在这里,我们不需要显式调用析构函数。当某个类对象无效时,编译器将自动安排调用该类的析构函数。本示例中,析构函数的调用发生在main()函数的执行结束之后,因此在main()函数安全终止之后因析构函数存在错误而使程序崩溃也是非常有可能的。

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有