placement new
#include <iostream>
using std::cout ;
using std::endl ;
class Cresource {
private:
static int m_ct ;
int m_i ;
public:
Cresource(int i) : m_i(i) {
m_ct++ ;
cout << "Cresource(" << m_ct << ") : " ;
}
~Cresource() {
cout << "~Cresource(" << m_ct << ") : " ;
m_ct-- ;
}
int i(void) const {
return m_i ;
}
} ;
int Cresource::m_ct = 0 ;
class C {
private:
Cresource* m_pResource ;
public:
C(int i=0) : m_pResource(new Cresource(i)) {
cout << "(" << this << ")->C()" << endl ;
if (i==-1){
cout << "コンストラクタ中で例外発生" << endl ;
throw "(例外発生のシミュレート)" ;
}
}
~C(void) {
cout << "(" << this << ")->~C() : " ;
delete m_pResource ;
}
void show(void) {
cout << "(" << this << ")->show() : C (" << m_pResource->i() << ")" << endl ;
}
void* operator new(size_t s) {
cout << "new = malloc(" << s << ") : " ;
return malloc(s) ;
}
void operator delete(void* p) {
cout << "delete = free(" << p << ") : " << endl ;
if (p) free(p) ;
}
void* operator new(size_t s, void* p) {
cout << "new(" << s << ", " << p << ") : " ;
return p ;
}
void operator delete(void* p, void* q){ る
cout << "delete(" << p << ", " << q << ")" << endl ;
} ;
} ;
typedef char Tbuf[sizeof(C)] ;
static const int bufNum = 4 ;
static Tbuf buf[bufNum] ;
static C* const pC = (C*)buf ;
static void init(void){
cout << "\n[init()]" << endl ;
for (int i=0 ; i<bufNum ; i++) {
new (buf+i) C(10+i) ;
}
}
static void run(void){
cout << "\n[run()]" << endl ;
for (int i=0 ; i<bufNum ; i++) {
(pC+i)->show() ;
}
}
static void done(void){
cout << "\n[done()]" << endl ;
for (int i=0 ; i<bufNum ; i++) {
(pC+i)->~C() ;
cout << endl ;
}
}
int main(void){
cout << "\n" << buf << " <= buf < " << buf+bufNum << endl ;
init() ;
run() ;
done() ;
cout << "\n[例外]" << endl ;
try {
new (buf) C(-1) ;
}catch (char* e){
cout << "例外をキャッチ : " << e << endl ;
}catch (...){
cout << "unknown exception" << endl ;
}
return 0 ;
}