Ok, here's my test code with virtual methods tolua++:
-------------------------- header ---------------------------------------
#pragma once
#include <string>
#include <vector>
// lua
extern "C"
{
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
}
typedef std::string String;
class MyClass
{
public:
MyClass(String name);
String getName();
virtual void overrideMe() {}
protected:
String name_;
};
void passMe(MyClass* myClass);
-------------------------- pkg---------------------------------------
$cfile "luatest.h"
$#include "tolua_base.h"
typedef std::string String;
class MyClass
{
public:
MyClass(String name);
String getName();
virtual void overrideMe();
protected:
String name_;
};
void passMe(MyClass* myClass);
-------------------------- source main ---------------------------------------
#include "stdafx.h"
#include " luatest.h"
#include "VSLua_Debugger.h"
#include <vector>
int tolua_luatest_open (lua_State* tolua_S);
lua_State* state_;
static int report (lua_State *L, int status) {
if (status && !lua_isnil(L, -1)) {
const char *msg = lua_tostring(L, -1);
if (msg == NULL) msg = "(error object is not a string)";
printf("%s\n", msg);
lua_pop(L, 1);
}
return status;
}
int _tmain(int argc, _TCHAR* argv[])
{
state_ = lua_open(); // initialize mLuaState for LUA
luaL_openlibs(state_);
tolua_luatest_open(state_);
int status = luaL_dofile(state_, " myFirst.lua");
if (status != 0)
{
report(state_, status);
assert(false);
}
return 0;
}
MyClass::MyClass(std::string name)
{
name_ = name;
}
String
MyClass::getName()
{
return name_;
}
void passMe(MyClass* myClass)
{
myClass->overrideMe();
}
-------------------------- lua ---------------------------------------
local MyDerivedClass = {}
MyDerivedClass.__index = MyDerivedClass
function MyDerivedClass:overrideMe()
print("overrideMe called in lua")
print(self:getName())
end
function MyDerivedClass:new(name)
-- create a table and make it an 'instance' of CustomWidget
local t = {};
setmetatable(t, MyDerivedClass);
-- create a Lua__MyClass object, and make the table inherit from it
local w = Lua__MyClass:new(name);
tolua.setpeer(w, t) -- use 'tolua.inherit' on lua 5.0
-- 'w' will be the lua object where the virtual methods will be looked up
w:tolua__set_instance(w);
return w; -- return 't' on lua 5.0 with tolua.inherit
end
instance = MyDerivedClass:new("nacho")
passMe(instance);
------------------------------------------------------------------------------------
output:
override me called in lua
nacho
Hope this helps :)
-------------------------- header ---------------------------------------
#pragma once
#include <string>
#include <vector>
// lua
extern "C"
{
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
}
typedef std::string String;
class MyClass
{
public:
MyClass(String name);
String getName();
virtual void overrideMe() {}
protected:
String name_;
};
void passMe(MyClass* myClass);
-------------------------- pkg---------------------------------------
$cfile "luatest.h"
$#include "tolua_base.h"
typedef std::string String;
class MyClass
{
public:
MyClass(String name);
String getName();
virtual void overrideMe();
protected:
String name_;
};
void passMe(MyClass* myClass);
-------------------------- source main ---------------------------------------
#include "stdafx.h"
#include " luatest.h"
#include "VSLua_Debugger.h"
#include <vector>
int tolua_luatest_open (lua_State* tolua_S);
lua_State* state_;
static int report (lua_State *L, int status) {
if (status && !lua_isnil(L, -1)) {
const char *msg = lua_tostring(L, -1);
if (msg == NULL) msg = "(error object is not a string)";
printf("%s\n", msg);
lua_pop(L, 1);
}
return status;
}
int _tmain(int argc, _TCHAR* argv[])
{
state_ = lua_open(); // initialize mLuaState for LUA
luaL_openlibs(state_);
tolua_luatest_open(state_);
int status = luaL_dofile(state_, " myFirst.lua");
if (status != 0)
{
report(state_, status);
assert(false);
}
return 0;
}
MyClass::MyClass(std::string name)
{
name_ = name;
}
String
MyClass::getName()
{
return name_;
}
void passMe(MyClass* myClass)
{
myClass->overrideMe();
}
-------------------------- lua ---------------------------------------
local MyDerivedClass = {}
MyDerivedClass.__index = MyDerivedClass
function MyDerivedClass:overrideMe()
print("overrideMe called in lua")
print(self:getName())
end
function MyDerivedClass:new(name)
-- create a table and make it an 'instance' of CustomWidget
local t = {};
setmetatable(t, MyDerivedClass);
-- create a Lua__MyClass object, and make the table inherit from it
local w = Lua__MyClass:new(name);
tolua.setpeer(w, t) -- use 'tolua.inherit' on lua 5.0
-- 'w' will be the lua object where the virtual methods will be looked up
w:tolua__set_instance(w);
return w; -- return 't' on lua 5.0 with tolua.inherit
end
instance = MyDerivedClass:new("nacho")
passMe(instance);
------------------------------------------------------------------------------------
output:
override me called in lua
nacho
Hope this helps :)