#include <iostream> using namespace std; class MyObject { public : MyObject(){ ① cout << "call constructor." << endl; } ~MyObject(){ ② cout << "call destructor." << endl; } void initialize(){ ③ cout << "call initialization." << endl; } void destroy(){ ④ cout << "call destroy." << endl; } }; int main(){ MyObject *obj = (MyObject *)malloc(sizeof(MyObject)); // 申请动态内存 ⑤ obj->initialize(); ⑥ //TODO obj->destroy(); ⑦ free(obj); ⑧ obj = NULL; return 0; }
#include <iostream> using namespace std; class MyObject { public : MyObject(){ cout << "call constructor." << endl; } ~MyObject(){ cout << "call destructor." << endl; } void initialize(){ cout << "call initialization." << endl; } void destroy(){ cout << "call destroy." << endl; } }; int main(){ MyObject *obj = new MyObject(); // 申请动态内存 //TODO delete obj; obj = NULL; return 0; }
call destructor.
原文地址:http://blog.csdn.net/tonny_guan/article/details/40717491