.cc把类传入lua

#include <iostream>
#include <luabind/luabind.hpp>
#include <luabind/function.hpp>

extern "C"
{
#include "lua5.1/lua.h"
#include "lua5.1/lualib.h"
#include "lua5.1/lauxlib.h"
}
using namespace std;

class NumberPrinter
{
public:
    NumberPrinter( int number )
        : m_number( number )
    {
    }
    void print()
    {
        cout << m_number << endl;
    }

private:
    int m_number;
};

int main()
{
    lua_State* myLuaState = luaL_newstate();
    luaL_openlibs( myLuaState );
    luabind::open( myLuaState );
    luabind::module( myLuaState )[luabind::class_<NumberPrinter>( "NumberPrinter" )
                                      .def( luabind::constructor<int>() )
                                      .def( "print", &NumberPrinter::print )];
    luaL_dofile( myLuaState, "demo3.lua" );
    lua_close( myLuaState );
}
Print2000=NumberPrinter(20000)
Print2000:print()
20000
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容