Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2140173
  • 博文数量: 556
  • 博客积分: 11457
  • 博客等级: 上将
  • 技术积分: 5973
  • 用 户 组: 普通用户
  • 注册时间: 2011-02-24 22:33
文章分类

全部博文(556)

文章存档

2013年(22)

2012年(74)

2011年(460)

分类: C/C++

2013-03-21 11:23:53


点击(此处)折叠或打开

  1. class obj
  2. {
  3. private :
  4.      char ch;    
  5. public:
  6.      obj(char c);
  7.          ~obj();
  8. };
  9. #include "iostream"
  10. using namespace std;

  11. void f();
  12. obj A('A');

  13. int main()
  14. {
  15.   cout<<"inside main()"<<endl;
  16.   f();
  17.   f();
  18.   cout<<"outside main()"<<endl;
  19.   //printf("outside main()");
  20.    return 0;
  21. }
  22. obj::obj(char c):ch(c)
  23. {
  24.   cout<<"construct ........."<<ch<<endl;
  25. }
  26. obj::~obj()
  27. {

  28.   printf("destruct...........");
  29.   printf("%c\n",ch);
  30.    //cout<<"destruct..........."<<ch<<endl;
  31. }
  32. void f()
  33. {
  34.   static obj B('B');
  35.   obj C('c');

  36. }
运行结果:

如果析构函数中采用cout进行打印,会出现:析构函数没有被自动调用的现象。
参考文章:

阅读(1080) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~