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
|
#include <iostream> #include <cstdlib> using namespace std;
class A{
public :
A( void ){
cout << "A::A()" << endl;
}
~A( void ){
cout << "A::~A()" << endl;
}
static void * operator new ( size_t size){
cout << "A::new" << endl;
void * pv = malloc (size);
return pv;
}
static void operator delete ( void * pv) {
cout << "A::delete" << endl;
free (pv);
}
}; int main( void ){
A* pa = new A;
delete pa;
return 0;
} |
结果为:
A::new
A::A()
A::~A()
A::delete
由此我们可以知道,new 对象时,先new在调用其构造,
delete对象的时,先调用其析构,再delete
本文转自神ge 51CTO博客,原文链接:http://blog.51cto.com/12218412/1866959