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 3988600
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T06:16:17+00:00 2026-05-20T06:16:17+00:00

Hello I have the following bit of code which seems to work, but I’m

  • 0

Hello I have the following bit of code which seems to work, but I’m not sure why – I’ve built a testclass as follows

class testclass {
    int ivalue;
public:
    int getivalue();
    void setivalue(int &v);
};

and then registered the testclass (bits left out for the actual functions but they’re pretty basic). It’s the registration of the metatables I’m not following. (etivalue and setivalue are c functions that call the class functions of the same name)

static const struct luaL_Reg arraylib_f [] = {
    {"new", new_testclass},
    {NULL, NULL}
};

static const struct luaL_Reg arraylib_m [] = {
    {"set", setivalue},
    {"get", getivalue},
    {NULL, NULL}
};

int luaopen_testclass (lua_State *L) {
    luaL_newmetatable(L, "LuaBook.testclass");
    lua_pushvalue(L, -1); /* duplicates the metatable */
    lua_setfield(L, -2, "__index");
    luaL_register(L, NULL, arraylib_m);
    luaL_register(L, "testclass", arraylib_f);
    return 1;
}

The bit I don’t understand is I’m adding the functions to the __index for the metatable but
when I run

a = testclass.new()
a:set(10)
print(a:get())

Then it works as expected. The bit I don’t understand is why the set is being called when I think I’ve loaded it in the __index metatable? Is that what I’ve done or something else?

tia

  • 1 1 Answer
  • 1 View
  • 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-05-20T06:16:18+00:00Added an answer on May 20, 2026 at 6:16 am
    int luaopen_testclass (lua_State *L) {
        luaL_newmetatable(L, "LuaBook.testclass"); //leaves new metatable on the stack
        lua_pushvalue(L, -1); // there are two 'copies' of the metatable on the stack
        lua_setfield(L, -2, "__index"); // pop one of those copies and assign it to
                                        // __index field od the 1st metatable
        luaL_register(L, NULL, arraylib_m); // register functions in the metatable
        luaL_register(L, "testclass", arraylib_f);
        return 1;
    }
    

    That code is equivalent to the example Lua code:

    metatable = {}
    metatable.__index = metatable
    metatable.set = function() --[[ stuff --]] end
    metatable.get = function() --[[ stuff --]] end
    

    I assume that ‘new_testclass’ C function sets the metatable “LuaBook.testclass” for the returned table.

    In your code you dont add functions to the metatable __index field. You assign pointer to metatable to that metatable’s field named __index, and you register set and get functions to it.

    Now, if you set that metatable to the value returned from ‘new_testclass’ function (which I assume you do) – lets call that value ‘foo’, and you call foo:set(10), than Lua:

    1. checks that there is no such field as ‘set’ in ‘foo’
    2. sees that ‘foo’ has a metatable
    3. looks at that metatable’s __index field – sees it’s a table
    4. checks if that table assigned to __index field has a field ‘set’ and it’s value is a function
    5. calls ‘set’ method passing ‘foo’ as self parameter

    I hope that this will help you figure out whats going on here.

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

Sidebar

Related Questions

Hello I have the following code which uses knockout.js, var allJobberDetailsArray = []; getAllJobberDetailsArray();
Hello I have the following code: if(_deviceSegmentCntrl.selectedSegmentIndex == 0) { deviceOne = [[NSString alloc]
Hello I have the following code namespace ConsoleApplication2 { class Program { static void
I have following code snippet: class ABC{ public: int a; void print(){cout<<hello<<endl;} }; int
hello i have just start using Raphael but i'm very confused in the following
I have the following bit of code that modifies an observable collection of 'screens'
Hello I have the following php code here: $username = trim(mysql_real_escape_string($username)); $password = md5(trim(mysql_real_escape_string($password)));
Hello I have the following php code, from the output $doc I want to
I have the following code: <rich:tabPanel switchType=client> <rich:tab name=tab1 label=tab1 onclick=alert('Hello');> ... </rich:tab> <rich:tab
Hello i have the following problem, I record a video using red5 like this:

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.