new delete的實現

#include <iostream>
#include <cstdlib>
using namespace std;ios

class Example
{
 int i;
public:
 Example()
 {
  cout << this << ",Example()" << endl;
 }this

 ~Example()
 {
  cout << this << ",~Example()" << endl;
 }spa

 void *operator new(size_t sz)
 {
  cout << "new (" <<sz << ")" << endl;
  return malloc(sz);
 }io

 void operator delete(void *p)
 {
  cout << "delete (" << p << ")" << endl;
  free(p);
 }
 void operator delete [](void *p)
 {
  cout << "delete (" << p << ")" << endl;
  free(p);
 }class

 void* operator new [](size_t sz)
 {
  cout << "delete (" << sz <<")" << endl;
  return malloc(sz);
 }stream

};co

int main()
{
 Example * p = new Example;
 delete p;
 Example * p2 = new  Example[5];
 delete[] p2;
 return 0;
}
let

相關文章
相關標籤/搜索