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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:33:28+00:00 2026-05-26T23:33:28+00:00

I’m trying to implement a object notation in Lua scripts. Here is what I

  • 0

I’m trying to implement a object notation in Lua scripts.
Here is what I succeeded to do with the C API:

  • I created a new global table “Test” as a class and added a field “new” in it, pointing to a function written in C
    i.e I can do this in lua code: “local obj = Test.new()” (it calls the function “new”)

  • The “new” C function creates and returns a new table, and registers functions in it as fields (e.g “add”, “count”…)
    i.e I can do this: “obj:add(“mike”)” and “obj:count()” (obj is passed as first arguments with the “:” notation)

2 questions:

1) Everything works as expected, but the thing I’m wondering is: What is the advantage of using metatables in my case?
I see everywhere that metatables can help me to achieve what I tried to do, but I don’t understand where they would be useful?
Adding fields to tables as methods isn’t correct?
How could metatables help me (If added as my tables metatables)?

2) In fact I’m trying to reproduce the behaviour of C++ in Lua here.
For example, when I write this in C++: “Test *obj = new Test();”
I expect C++ and the constructor of Test to return me a pointer of an instance of Test.
This is exactly what I’m trying Lua to do for me.

The thing is that I use a table in this case, as the return of “new”, but not a pointer so I can call methods on it later with Lua (using its fields), like a standard C++ object (with the operator ->).

To be able to retreive the actual pointer of my class in the C fonctions, I added a field “ptr” (light uservalue) to the table returned by “new”. Without it, I would have been able to manipulate only the Lua table in my C function, nothing more (so no more method calls on the real pointer).

My second question is, Is it the right way to do it?
Do you have better idea on how to be able to manipulate my pointer everywhere without this “ptr” field?

Thank you,

Nicolas.

  • 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-05-26T23:33:29+00:00Added an answer on May 26, 2026 at 11:33 pm

    The main reason is that you get the __index metamethod.
    Without it, every instance of a object has to have all the functions associated with it: which can make tables why large; and use a lot of memory.

    local methods = { 
        foo = function() return "foo" end ;
        bar = function() return "bar" end ;
        bob = function() return "bob" end ;
        hen = function() return "hen" end ; 
    }
    

    So you have either

    function new_no_mt ( )
        local t = {}
        for k , v in pairs ( methods ) do t [ k ] = v end
        return t
    end
    

    vs

    local mt = { __index = methods ; }
    function new_mt ( )
        return setmetatable ( { } , mt )
    end
    

    There are other benefits too;

    • defining operators;
    • easy type comparison via comparing metatables.
    • Changing method in metatable changes it for all objects, but changing it on one object only changes it for that object.

    Otherwise, what you are trying to do sounds like the perfect situation for userdata.
    userdata can only be indexed when you have an __index metatmethod (theres no table to put methods in)

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

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
i got an object with contents of html markup in it, for example: string

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.