.cc把类传入lua

#include 
#include 
#include 

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" )
                                      .def( luabind::constructor() )
                                      .def( "print", &NumberPrinter::print )];
    luaL_dofile( myLuaState, "demo3.lua" );
    lua_close( myLuaState );
}
Print2000=NumberPrinter(20000)
Print2000:print()
20000

你可能感兴趣的:(.cc把类传入lua)