#include "thing.h"
void function(Thing t) {
Thing lt(106);//函數(shù)結(jié)束時(shí) 調(diào)用析構(gòu)
Thing* tp1 = new Thing(107);
Thing* tp2 = new Thing(108);// 不會(huì)調(diào)用析構(gòu)
delete tp1;
}
int main() {
Thing t1(101), t2(102); // 在main 函數(shù)結(jié)束時(shí) 調(diào)用析構(gòu)
Thing* tp1 = new Thing(103);
function(t1);// 其中t1 在function 結(jié)束時(shí)調(diào)用析構(gòu)
{ /* nested block/scope */
Thing t3(104);// 該作用域結(jié)束時(shí) 調(diào)用析構(gòu)
Thing* tp = new Thing(105);// 不會(huì)調(diào)用析構(gòu)
}
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
運(yùn)行結(jié)果
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
付翔 閱讀(176)
評論(0) 編輯 收藏 引用 所屬分類:
c++