On Wed, May 16, 2012 at 12:14 AM, Patrick Rapin
<toupie300@gmail.com> wrote:
One could then get a step further and specify the metatable registry
name typically associated with the userdata.
void * operator new( size_t size , lua_State * L, const char* metaname )
{
void* obj = lua_newuserdata( L , size );
luaL_getmetatable(L, metaname);
lua_setmetatable(L, -2);
return obj;
}
I usually delay setting the metatable until the object is in a defined state, which is hard to do with this idiom. If the constructor of the object throws a C++ exception and you catch it the "__gc" routine the metatable refers to may call the destructor, which is a bad idea because the constructor failed.
--