重载new和delete

1. 测试代码:

 1 #include<iostream>
 2 #include<new>
 3 using namespace std;
 4 class A {
 5 public:
 6     A() { cout << "A constructor" << endl; }
 7 
 8     void* operator new(size_t size) 
 9     {
10         cout << "this is A's new" << endl;
11         return ::operator new(size);
12     }
13 
14     void operator delete(void* ptr) 
15     {
16         cout << "this is A's delete" << endl;
17         return ::operator delete(ptr);
18     }
19 
20     ~A() {  cout << "A destructor" << endl; }
21 };
22 
23 int main()
24 {
25     A *a = new A;
26     delete a;
27     return 0;
28 }
View Code

相关文章:

  • 2021-09-08
  • 2021-11-01
  • 2021-07-02
  • 2022-12-23
  • 2021-07-03
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-24
  • 2022-02-15
  • 2022-12-23
相关资源
相似解决方案