#includeusing namespace std; class String{ char* p; public: String(){ p = new char[10]; } ~ String(); }; String::~ String(){ delete []p; } //对象数组生命周期结束时,对象数组的每个元素都会调用析构函数 class Ctest{ public: ~Ctest(){ cout << "hello" << endl; } }; int main(){ Ctest array[2]; cout << "main" << endl; Ctest * p = new Ctest; delete p;//delete时也会调用析构函数 return 0; } //输出: class CMyclass{ public: ~CMyclass(){}; }; CMyclass obj; CMyclass fun(CMyclass sobj){//参数对象消亡调用析构函数 return sobj;//函数调用返回时生成临时对象返回 } int main(){ obj = fun(obj);//函数调用的返回值被使用后,该临时对象调用析构函数 return 0; }



