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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T20:37:15+00:00 2026-05-14T20:37:15+00:00

I want to have C++ objects that I can read/write in both C++ &

  • 0

I want to have C++ objects that I can read/write in both C++ & Lua.

I have looked at: http://www.lua.org/pil/28.html

However, I do not like that solution, since my objects have constructors & destructors (and they are important as I use RAII and these take care of reference counts).

What I don’t like in the PIL solution is that the object is allocated in Lua’s heap.

What i want instead, is to create hte C++ object on my own, and just have lua have a way to do get/set on them.

Does anyone have a good tutorial/link on this?

Tanks!

  • 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-14T20:37:15+00:00Added an answer on May 14, 2026 at 8:37 pm

    One option is to use a light userdata, which allows you to allocate the object on the C++ heap. See the documentation for lua_pushlightuserdata. Unfortunately a light userdata has no metatable. Even if you are willing to access it using Lua functions to do get/set you still have to do something like this:

    static int myget(lua_State *L) {
       myclass thing = lua_touserdata(L, 1);
       lua_pushnumber(thing::getsize());
       return 1;
    }
    

    Unfortunately, because it’s a light userdata, there’s no real way to make this operation type-safe—all light userdata are treated the same, and they have no metatable.

    The better solution is to allocate a full userdata on the Lua heap, with a proper metatable, whose contents are a single pointer to the object allocated on the C++ heap. Then you can follow the model in Programming in Lua. For examples you can look at the Lua io library to see how FILE * is handled. This way you can write your C interfaces using the luaL_checkudata function, and they will be safe, but you still will have the right to allocate your objects on the C++ heap instead of on the Lua heap.

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

Sidebar

Ask A Question

Stats

  • Questions 382k
  • Answers 382k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer A more pleasant option (for debugging) is to add a… May 14, 2026 at 10:37 pm
  • Editorial Team
    Editorial Team added an answer If you really store the date as a string in… May 14, 2026 at 10:37 pm
  • Editorial Team
    Editorial Team added an answer var classNames = $('#dv').attr('class').split(' '); if you just need to… May 14, 2026 at 10:37 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.