#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
付翔 閱讀(173)
評論(0) 編輯 收藏 引用 所屬分類:
c++