virtual methods in tolua++

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 :)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值