#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
付翔 阅读(161)
评论(0) 编辑 收藏 引用 所属分类:
c++