Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9234301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:45:29+00:00 2026-06-18T06:45:29+00:00

How to return C++ object to lua? My C++ code is following: class MyClass

  • 0

How to return C++ object to lua?

My C++ code is following:

class MyClass
{
public:
  void say()
  {
    print("Hello\r\n");
  }
};

int test(lua_State* l)
{
  MyClass* obj = new MyClass();
  lua_pushlightuserdata(l, obj);
  return 1;
}

Lua Test is following:

local a = MyClass:new()
a:say()  <--- OK, beacause I set metatable!!
local b = test()
b:say()  <--- ERROR: attempt to index local 'b' (a userdata value)

How to modify test() function to work fine?
obj will auto destory by lua ?

PS: I has set MyClass metatable is following

void l_registerClass()
{
  lua_newtable(l);
  int methods = lua_gettop(l);
  luaL_newmetatable(l, "MyClass");
  int metatable = lua_gettop(l);
  lua_pushvalue(l, methods);
  lua_setglobal(l, "MyClass");

  lua_pushvalue(l, methods);  
  l_set(l, metatable, "__metatable");

  //set metatable __index
  lua_pushvalue(l, methods);
  l_set(l, metatable, "__index");

  //set metatable __gc
  lua_pushcfunction(l, l_destructor);  
  l_set(l, metatable, "__gc");

  //set method table
  lua_newtable(l);                // mt for method table  
  lua_pushcfunction(l, l_constructor);  
  lua_pushvalue(l, -1);           // dup new_T function  
  l_set(l, methods, "new");         // add new_T to method table  
  l_set(l, -3, "__call");           // mt.__call = new_T  
  lua_setmetatable(l, methods);  

  // set methods metatable   
  lua_pushstring(l, "say");
  lua_pushcclosure(l, l_proxy, 1);  
  lua_settable(l, methods);

  lua_pop(l, 2);
}

int l_proxy(lua_State* l)
{
  int i = (int)lua_tonumber(l, lua_upvalueindex(1));
  lua_remove(l, 1);  // remove self so member function args start at index 1
  //call real function
  MyClass* obj = getInstance();
  obj->say();
  return 1;
}

I should don’t lost step ?
I don’t use any Lua Binding Framework, I am using pure Lua Library.

==== update 1 ====

Thanks for user1520427’s answer, but….

int test(lua_State* l)
{
  MyClass** c = (MyClass**)lua_newuserdata(l, sizeof(MyClass*));
  *c = new MyClass();       // we manage this
  lua_getglobal(l, "MyClass");
  lua_setmetatable(l, -2);
  return 1;
}

and I test it in Lua

local b = test()
print( type(b) )
local meta = getmetatable(b)
for k,v in pairs(meta) do
  print("    ", k, v)
end

Lua show metatable is correct.

userdata
  say     function: 00602860
  new     function: 00493665

But lua still shows the same error in

b:say()   <-- attempt to index local 'b' (a userdata value)

=== update 2 ===

int test(lua_State* l)
{
  MyClass** c = (MyClass**)lua_newuserdata(l, sizeof(MyClass*));
  *c = new MyClass();       // we manage this
  luaL_getmetatable(l, "MyClass");  //
  lua_getglobal(l, "MyClass");
  lua_setmetatable(l, -2);
  return 1;
}

the lua test result:

b:say()   <-- attempt to call method 'say' (a nil value)

=== update 3 ===

int test(lua_State* l)
{
  MyClass** c = (MyClass**)lua_newuserdata(l, sizeof(MyClass*));
  *c = new MyClass();       // we manage this
  luaL_getmetatable(l, "MyClass");  
  luaL_setmetatable(l, "MyClass");  //modify
  return 1;
}

Lua test result:

b:say()   <-- calling 'say' on bad self
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T06:45:30+00:00Added an answer on June 18, 2026 at 6:45 am

    thanks for user1520427 and lhf
    correct code sould is following:

    int test(lua_State* l)
    {
      MyClass** c = (MyClass**)lua_newuserdata(l, sizeof(MyClass*));
      *c = new MyClass();       // we manage this
      luaL_setmetatable(l, "MyClass");  //assign MyClass metatable
      return 1;
    }
    

    Lua test code is work fine.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In C++ code: class CWindowUI { public CWindowUI(const char* title,int width,int height); ..... };
class A(object): def __cmp__(self): print '__cmp__' return object.__cmp__(self) def __eq__(self, rhs): print '__eq__' return
Can a class return an object of itself. In my example I have a
Suppose I have a method that must return an object (say from a database
In Lua Code Test = {} function Test:new() local obj = {} setmetatable(obj, self)
Requirement : Return the element object. Problem : Using the code below, I expected
I found a segment of java code that is claimed to return object using
I need to create class that should return object which is build from this
I have a class that uses XML and reflection to return Object s to
Okay, so I've got a strange problem with the following Lua code: function quantizeNumber(i,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.