付翔的专栏
在鄙视中成长 记录成长的点滴
posts - 106,  comments - 32,  trackbacks - 0
#include "thing.h"

void function(Thing t) {
    Thing lt(106);//函数结束时 调用析构 
    Thing* tp1 = new Thing(107);
    Thing* tp2 = new Thing(108);// 不会调用析构
    delete tp1;
}

int main() {
    Thing t1(101), t2(102); // 在main 函数结束时 调用析构
    Thing* tp1 = new Thing(103); 
    function(t1);// 其中t1 在function 结束时调用析构
    {  /* nested block/scope */
        Thing t3(104);// 该作用域结束时 调用析构
        Thing* tp = new Thing(105);// 不会调用析构 
    }
    delete tp1;
    return 0;
}

#ifndef THING_H_
#define THING_H_

#include <iostream>
#include <string>
using namespace std;

class Thing {
 public:
    Thing(int n) : m_Num(n) {
        
    }
    ~Thing() {
        cout << "destructor called: " 
             << m_Num << endl;
    }
    
 private:
    string m_String;
    int m_Num;
};
#endif

运行结果
destructor called: 107
destructor called: 106
destructor called: 101
destructor called: 104
destructor called: 103
destructor called: 102
destructor called: 101
posted on 2011-02-27 21:23 付翔 阅读(148) 评论(0)  编辑 收藏 引用 所属分类: c++

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理



<2011年2月>
303112345
6789101112
13141516171819
20212223242526
272812345
6789101112

常用链接

留言簿(2)

随笔分类

随笔档案

文章分类

文章档案

CSDN - 我的blog地址

博客

搜索

  •  

最新评论

阅读排行榜

评论排行榜