小编use*_*268的帖子

如何从Lua中提取C++对象指针

我有一个C++类叫做"Point":

class Point
{
public:
  int x, y;

  //constructor
  Point(int x, int y)
  {
    this->x = x;
    this->y = y;
  }
};
Run Code Online (Sandbox Code Playgroud)

我的目标是能够使用Lua脚本实例化Point对象,并从Lua堆栈中提取指向此对象的指针.

这是我(目前没有工作)的尝试,希望澄清我到底想要做什么; 请注意,此代码基本上是从本教程中修改的复制/粘贴,并且我使用的是Lua 5.2:

static int newPoint(lua_State *L) 
{
    int n = lua_gettop(L);
    if (n != 2) 
       return luaL_error(L, "expected 2 args for Point.new()", n); 

    // Allocate memory for a pointer to object
    Point **p = (Point **)lua_newuserdata(L, sizeof(Point *));  

    double x = luaL_checknumber (L, 1);      
    double y = luaL_checknumber (L, 2); 

    //I want to access …
Run Code Online (Sandbox Code Playgroud)

c++ lua

9
推荐指数
2
解决办法
2772
查看次数

标签 统计

c++ ×1

lua ×1