-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathnewhacks.cpp
More file actions
51 lines (38 loc) · 1.22 KB
/
newhacks.cpp
File metadata and controls
51 lines (38 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include "newhacks.h"
using namespace std;
Foo::Foo() : cat(), frog() { cerr << "constructed Foo" << this << endl; }
Foo::~Foo() { cerr << "destructed Foo" << this << endl; }
void *Foo::operator new(size_t size) {
void *pVoid = malloc(size);
cerr << "operator new(" << size << ") returns " << pVoid << endl;
return pVoid;
}
void Foo::operator delete(void *pVoid) {
cerr << "operator delete(" << pVoid << ")" << endl;
free(pVoid);
}
Bar::Bar() : turtle(), dog() { cerr << "constructed Bar " << this << endl; }
Bar::~Bar() { cerr << "destructed Bar" << this << endl; }
void *Bar::operator new(size_t size) {
void *pVoid = malloc(size);
cerr << "operator new(" << size << ") returns " << pVoid << endl;
return pVoid;
}
void Bar::operator delete(void *pVoid) {
cerr << "operator delete(" << pVoid << ")" << endl;
free(pVoid);
}
Buz::Buz() : snake(), rhinoceros() { cerr << "constructed Buz" << this << endl; }
Buz::~Buz() { cerr << "destructed Buz " << this << endl; }
int main()
{
Foo foo_stack = Foo();
Bar bar_stack = Bar();
Buz buz_stack = Buz();
Foo* foo_heap = new Foo();
Bar* bar_heap = new Bar();
delete(foo_heap);
delete(bar_heap);
return 0;
}