#include
using namespace std;
struct a
{
void operator new(size_t size)
{
cout << "new" << endl;
return malloc(size);
}
void operator delete (void mem)
{
cout << "delete" << endl;
free(mem);
}
a(){cout << "a::ctor()" << endl;};
~a(){cout << "a::dtor()" << endl;};
};
struct b
{
void operator new(size_t size)
{
cout << "new" << endl;
return malloc(size);
}
void operator delete (void mem)
{
cout << "delete" << endl;
free(mem);
}
b(){ m_a = new a();cout << "b::ctor()" << endl;};
~b(){delete m_a;cout << "b::dtor()" << endl;};
a m_a;
};
struct c
{
c(){cout << "c::ctor()" << endl;};
~c(){ cout << "c::dtor()" << endl;};
b m_b;
};
int main()
{
c c = new c();
// デストラクト
c->~c();
//placement newで最初期化
c = new (c)c();
// もう一回デストラクト
c->~c();
// メモリを廃棄
operator delete(c);
}
結果:
new
a::ctor()
b::ctor()
c::ctor()
c::dtor()
a::dtor()
delete
b::dtor()
new
a::ctor()
b::ctor()
c::ctor()
c::dtor()
a::dtor()
delete
b::dtor()