一道和同学讨论的C++编程题目

1210阅读 0评论2013-03-21 高傲的活着
分类:C/C++


点击(此处)折叠或打开

  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进行打印,会出现:析构函数没有被自动调用的现象。
参考文章:

上一篇:Java7中的switch支持String的实现细节
下一篇:电池电量监控开题报告文档