????2??new??delete???
??????malloc??free?????new??delete?????????????C++?????????new?????????????????????????裨?????????????????????棬??????????ù????????????棩?????????ù??????????????
????MyObject *obj = new MyObject();
???????????????????????????????????б??????????????????????г????????
????new??????????????????delete??delete??????????????????????檔???????
????delete obj;
????obj????????obj?????????new??????????????????malloc??????????????delete??????????????obj=NULL????“????”??
???????  ?????????????????б????????????????????????????????????????NULL?????????if????????????????Ч?????????????????????free????delete????????????????????????????????????а???????????????????????“????”??檔???????if?????????????????Ч???“????”????Σ???????????????????????????????????????????NULL?????????“????”???????
???????new??delete??????????????????????????£?
#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;
}
?????????MyObject???????new??????棬delete?????檔??????л???ù???????????????????н?????????£?
????call constructor.
????call destructor.