博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
new delete的实现
阅读量:6994 次
发布时间:2019-06-27

本文共 707 字,大约阅读时间需要 2 分钟。

hot3.png

#include <iostream>

#include <cstdlib>
using namespace std;

class Example

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

 ~Example()

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

 void *operator new(size_t sz)

 {
  cout << "new (" <<sz << ")" << endl;
  return malloc(sz);
 }

 void operator delete(void *p)

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

 void* operator new [](size_t sz)

 {
  cout << "delete (" << sz <<")" << endl;
  return malloc(sz);
 }

};

int main()

{
 Example * p = new Example;
 delete p;
 Example * p2 = new  Example[5];
 delete[] p2;
 return 0;
}

转载于:https://my.oschina.net/u/142173/blog/67390

你可能感兴趣的文章