Interesting C++ way to allocate user data (placement new)
[
Date Prev][
Date Next][
Thread Prev][
Thread Next]
[
Date Index]
[
Thread Index]
- Subject: Interesting C++ way to allocate user data (placement new)
- From: Pablo Pissanetzky <pablo@...>
- Date: 2012年5月15日 17:32:58 -0700
Just wanted to share this idea:
void * operator new( size_t size , lua_State * L )
{
return lua_newuserdata( L , size );
}
Then:
Foo * foo = new ( L ) Foo( constructor arguments );
Of course, you can never:
delete foo;
And in __gc you have to:
( static_cast< Foo * >( lua_touserdata( L , 1 ) ) )->~Foo();
It looks elegant but also dangerous ( in a good way, I think :)
P.S. You also have to have a delete operator to deal with exceptions.